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

**URL:** <https://www.ruby-forum.com/t/why-eval-is-not-failing-in-the-third-case-c3/158061>\
**Category:** Ruby\
**Created:** [January 18, 2009, 10:36pm UTC](https://www.ruby-forum.com/t/why-eval-is-not-failing-in-the-third-case-c3/158061 "2009-01-18T22:36:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [January 18, 2009, 10:36pm UTC](https://www.ruby-forum.com/t/why-eval-is-not-failing-in-the-third-case-c3/158061/1 "2009-01-18T22:36:52Z")

</div>

> > c1 = ‘hello’  
> > eval c1  
> > NameError: undefined local variable or method `hello’ for main:Object

> > c2 = ‘hello #{hi}’  
> > =\> “hello #{hi}”
> 
> > eval c2  
> > NameError: undefined local variable or method `hello’ for main:Object

> > c3 = ‘#{hi} hello’  
> > =\> “#{hi} hello”
> 
> > eval c3  
> > =\> 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 ?

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [January 18, 2009, 10:58pm UTC](https://www.ruby-forum.com/t/why-eval-is-not-failing-in-the-third-case-c3/158061/2 "2009-01-18T22:58:58Z")

</div>

2009/1/18 Raj S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> =\> “#{hi} hello”
> 
> > > eval c3  
> > > =\> 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”

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [January 19, 2009, 1:42am UTC](https://www.ruby-forum.com/t/why-eval-is-not-failing-in-the-third-case-c3/158061/3 "2009-01-19T01:42:20Z")

</div>

On Sun, 18 Jan 2009 16:35:31 -0500, Raj S. wrote:

> =\> “#{hi} hello”
> 
> > > eval c3  
> > > =\> 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.
