-5

i have a method declaration with one argument :

def my_method(argum)
   if argum.empty?
      puts "argument is empty"
   else 
      puts "argument is not empty"
   end
end

when i call this method and i pass a symbol to it like : my_method(:aleatoir_symbol) it show me argument is not empty and when i pass a literal symbol to it like this my_method(:"") it show argument is empty

i also test with irb and this is the result :

:a_symbol.empty?
 => false
 :"".empty?
 => true

my question is why when i pass a symbol like :any_symbol it show argument is not empty ??

i'm searching and i find a similar question here but there are just one answer who given't me a clear comprehension of the reason. any clear answer will be helpful . thank's

** update **


here is my original question, and i open this question because i don't have a good answer

Community
  • 1
  • 1
medBouzid
  • 7,484
  • 10
  • 56
  • 86
  • 2
    And what is the question? – Marek Lipka Jul 25 '13 at 11:55
  • I don't understand your question. In my opinion, this behaviour is fully expected. – Marek Lipka Jul 25 '13 at 12:04
  • my :symbol is not assigned with any value , i can't understand why is not empty ?! – medBouzid Jul 25 '13 at 12:06
  • 2
    What does "`symbol` not assigned with any value" mean? – Marek Lipka Jul 25 '13 at 12:07
  • Think of it this way: the name of the symbol *is* the value. – Mark Thomas Jul 25 '13 at 12:08
  • in rails for example i use a provide helper ex: provide(:title, "title of page") , and i have a method who take one argument like the method above : my_method(yield(:title), and test if this argument is empty, when i don't use provide(:title, "title of page") it take argument as nonempty – medBouzid Jul 25 '13 at 12:12
  • 1
    It's not "assigning value to symbol". It's passing more than one argument to method. – Marek Lipka Jul 25 '13 at 12:13
  • passing more than one argument to method ? what this means ? – medBouzid Jul 25 '13 at 12:20
  • It means it's a method that takes more than one parameters, like this: `def mymethod(a, b) puts [a,b]; end`. This is a method that takes two arguments and prints them on stdout. If you want to call it, you type for example `mymethod(:foo, :bar)`. – Marek Lipka Jul 25 '13 at 12:22
  • but my method take just one argument, i'm not a beginner lol perhaps my english is bad :( also i don't understand why people who are reading my question likes do -1 , i think this is for encouragement – medBouzid Jul 25 '13 at 12:30
  • @medBo I gave you downvote because your question is very difficult (or even impossible) to understand. I guess the others did it because of the same reason. You should be more clear. Look at the number of comments we produced to clarify the case and still no one knows exactly what you are asking for. Still I don't understand why you expect `:symbol.empty?` to be `false`. – Marek Lipka Jul 25 '13 at 12:33
  • symbol is not an identifier. – Santhosh Jul 25 '13 at 12:36

1 Answers1

2

Symbol#empty is basically defined as self.to_s.empty? (with self being the symbol).

So to answer your question why :"".empty? is true: It is because :"".to_s (the empty string) is empty.

To adress your comment: :any_symbol.empty? is false, because :any_symbol.to_s.empty? is false. It's the same thing. Maybe empty? is not the method you are looking for. Maybe I just didn't get what you are asking.

tessi
  • 13,313
  • 3
  • 38
  • 50
  • my question is why :any_symbol.empty? is nonempty ( don't ask about :"" ) – medBouzid Jul 25 '13 at 12:14
  • @medBo Why it should be? Why do you expect `:any_symbol.empty?` to return `false`? – Marek Lipka Jul 25 '13 at 12:17
  • i know that symbol is not a variable , and is used to design something not to have a value, but i'm confusing when i call a method who test if title passed to it is empty or not : full_title(yield(:title)) , here if i dont provide a title with provide(:title, "value"), it still nonempty – medBouzid Jul 25 '13 at 12:27
  • @medBo I read your comment twice and I still don't understand why you expect `:any_symbol.empty?` to return `false`. Please be more clear. – Marek Lipka Jul 25 '13 at 12:30