----------------------------------- richcash Sat Nov 11, 2006 11:42 pm 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) 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? ----------------------------------- [Gandalf] Sun Nov 12, 2006 12:26 am ----------------------------------- 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: 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. ----------------------------------- [Gandalf] Sun Nov 12, 2006 12:37 am ----------------------------------- "]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: 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. ----------------------------------- Cervantes Sun Nov 12, 2006 12:39 am ----------------------------------- "] 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 >> "#{foo = 5}" => "5" >> foo => 5 ----------------------------------- richcash Sun Nov 12, 2006 1:02 am ----------------------------------- Ah yes, it makes sense now. Thank you both for replying! :D ----------------------------------- [Gandalf] Sun Nov 12, 2006 1:08 am ----------------------------------- No problem richcash, Ruby is a lot funner to give help in (or try to) than some other stuff. :) But >> "#{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: puts "Ruby says, \"# { There shouldn't be a space between the # and {. I have a long ways to come yet. :P ----------------------------------- Cervantes Sun Nov 12, 2006 12:28 pm ----------------------------------- "] 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. ----------------------------------- wtd Sun Nov 12, 2006 12:34 pm ----------------------------------- In all likelihood, the confusion here stems from me writing while nearly asleep at some point. :)