|
Navigation |
Ruby /
Gotchas
|
x = 2 if x == 1 puts 'one' elseif x == 2 puts 'two' else puts 'many' end | Output:manyExplanation: Because elsif is misspelled, it looks like an ordinary method call. Syntax coloring helped me find this error, because elseif wasn't the same color as the if and else keywords. |
def one two end one # Error here! def two puts "success!" end one # This is ok. | Results: The call to one in the fifth line results in an error message saying that method two is undefined; but if you comment out that line, the call to one in the last line will print "success!".Explanation: In Java, method definition happens at compile time; but in Ruby, def is an executable statement; that is, the method gets defined when you execute the def. |