Computer Science Canada

Does the 'empty?' method work?

Author:  richcash [ Sat Nov 11, 2006 11:42 pm ]
Post subject:  Does the 'empty?' method work?

When I try to execute this code, it gives me an error after I type in any name saying undefined method 'empty?' for nil:NilClass (NoMethodError)
code:
puts "Hello, Ruby!"
print "I'm "
name = gets.strip

puts "Ruby says, \"#{statement = case name when "" then "fine, ignore me!" when /Calvin|Linus|Sid/ then "Haaaaa haaaaa!" else "Hello #{name}" end}\"" unless statement.empty?

This code is from wtd's second tutorial, and the empty? method is supposed to return true when statement holds "", false otherwise.

Does anyone know why it doesn't work?

Author:  [Gandalf] [ Sun Nov 12, 2006 12:26 am ]
Post subject: 

Is that exactly from the tutorial? Because the formatting of that code could sure use improvement.

Anyway, here it is a bit more organized and logical to the best of my ability:
Ruby:
puts "Hello, Ruby!"
print "I'm "
name = gets.strip
puts "Ruby says, \"#{
  statement = case name
  when "
" then
    "
fine, ignore me!"
  when /Calvin|Linus|Sid/ then
    "
Haaaaa haaaaa!"
  else "
Hello #{name}"
end }\""

I don't see any purpose to you having the empty? method there at all. The error message you were getting was because statement only exists withing that block you have there, but you're empty? method is outside it.

Author:  [Gandalf] [ Sun Nov 12, 2006 12:37 am ]
Post subject: 

[Gandalf] wrote:
Is that exactly from the tutorial?

Ah so it is, so it is. Well, I sure don't know as much as wtd, but the explanation for the error remains.

Personally, after a certain point of jamming code on one line, I get confused and make an attempt to organize it into clear steps like so:
code:
puts "Hello, Ruby!"
print "I'm "
name = gets.strip
puts "Ruby says, \"# {
  statement = case name
    when "" then
      "fine, ignore me!"
    when /Calvin|Linus|Sid/ then
      "Haaaaa haaaaa!"
    else "Hello #{name}"
  end
}\""

Note: This is the same code as in the above post with a slight improvement.

Author:  Cervantes [ Sun Nov 12, 2006 12:39 am ]
Post subject: 

[Gandalf] wrote:

The error message you were getting was because statement only exists withing that block you have there, but you're empty? method is outside it.


Not quite. The reason it gives an error is because the statement variable hasn't been given any value yet, so when it's first introduced in the statement.empty?, it's nil.

So, the statement.empty? is executed before the string stuff is executed. Why? Well, of course it has to be, since the puts line should be executed unless statement.empty? is true. We have to determine whether statement.empty? is true before doing any of the stuff in the string.

But [Gandalf], the instance variable exists outside of the string:
code:

>> "#{foo = 5}"
=> "5"
>> foo
=> 5

Author:  richcash [ Sun Nov 12, 2006 1:02 am ]
Post subject: 

Ah yes, it makes sense now. Thank you both for replying! Very Happy

Author:  [Gandalf] [ Sun Nov 12, 2006 1:08 am ]
Post subject: 

No problem richcash, Ruby is a lot funner to give help in (or try to) than some other stuff. Smile

Cervantes wrote:
But [Gandalf], the instance variable exists outside of the string:
code:

>> "#{foo = 5}"
=> "5"
>> foo
=> 5

Ah, I see. Ok then, but still, why have it there at all? You're already handling if the input is empty within the case structure, no?

And one last thing, just for future reference, in my last code:
code:
puts "Ruby says, \"# {

There shouldn't be a space between the # and {. I have a long ways to come yet. Razz

Author:  Cervantes [ Sun Nov 12, 2006 12:28 pm ]
Post subject: 

[Gandalf] wrote:

Ah, I see. Ok then, but still, why have it there at all? You're already handling if the input is empty within the case structure, no?


Why have a statement.empty? test at all? Good point. What's more, why have a statement variable at all? It's assigned to the value returned by the case expression, then never used again.

Author:  wtd [ Sun Nov 12, 2006 12:34 pm ]
Post subject: 

In all likelihood, the confusion here stems from me writing while nearly asleep at some point. Smile


: