Why eval is not failing in the third case ( c3)

R

Raj Singh

c1 = 'hello'NameError: undefined local variable or method `hello' for main:Object
NameError: undefined local variable or method `hello' for main:Object
=> nil


My question is why the eval of c3 is returning nil. It should fail
saying that undefined local variable just as it did in the case of c2 ?
 
T

Time World

[Note: parts of this message were removed to make it a legal post.]

2009/1/18 Raj Singh said:
NameError: undefined local variable or method `hello' for main:Object

NameError: undefined local variable or method `hello' for main:Object

=> nil


My question is why the eval of c3 is returning nil. It should fail
saying that undefined local variable just as it did in the case of c2 ?


It's evaluated as a comment:
#hi hello
If you want #{hi} to be replaced by the content of the variable hi, you
should use double quotes:
c4 = "#{hi} hello"
 
K

Ken Bloom

NameError: undefined local variable or method `hello' for main:Object

NameError: undefined local variable or method `hello' for main:Object

=> nil


My question is why the eval of c3 is returning nil. It should fail
saying that undefined local variable just as it did in the case of c2 ?

In the second and third cases, everything after (and including) the # is
interpreted as a comment. Thus, you're executing

hello
hello #{hi}
#{hi} hello

In case 1, hello is a undefined method, so a NameError is raised
In case 2, hello comes before the comment, so it gets executed (with no
arguments). A NameError is raised because it's an undefined method.
In case 3, hello is part of the comment. Nothing gets executed, so no
NameError is raised.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top