Why is this not a syntax error?

J

Jamey Cribbs

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.
 
T

ts

J> if a == 0
J> puts 'zero'
J> elif a == 1
J> puts 'one'
J> else
J> puts 'not one'
J> 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


Guy Decoux
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Why is this not a syntax error?"

|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

It's seen as

if a == 0
puts 'zero'
elif(a == 1)
puts 'one'
else
puts 'not one'
end

See?

matz.
 
J

Jamey Cribbs

ts said:
J> if a == 0
J> puts 'zero'
J> elif a == 1
J> puts 'one'
J> else
J> puts 'not one'
J> 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.
 
J

Joel VanderWerf

Jamey said:
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

Syntax hilighting in your editor works wonders on this kind of butt-bite.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,099
Latest member
AmbrosePri
Top