Switching back and forth between Python and Ruby bit me in the butt today. For example, consider the following Ruby code:
a = 1
if a == 0
puts 'zero'
elif a == 1
puts 'one'
else
puts 'not one'
end
The code in error is the "elif" statement. "elif" is python's else-if statement, as opposed to Ruby's elsif statement.
Now, I would expect the above Ruby code to generate a syntax error stating that "elif" is bad. However, when I run the above code, instead of a syntax error, the code executes and returns "not one", so it is not erroring out on the "elif" and it is just falling through to the else statement.
Why is this not a syntax error?
I googled for "Ruby elif" but did not find anything relevant.
Jamey Cribbs
Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.
if a == 0
puts 'zero'
elif a == 1
puts 'one'
else
puts 'not one'
end
You have just written
if a == 0
puts 'zero'
elif(a == 1) # call the method elif
puts 'one'
else
puts 'not one'
end
run it with a = 0, to see the error message
Gotcha!
Thanks, Guy.
Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.