# Eval bug?

**URL:** <https://www.ruby-forum.com/t/eval-bug/139855>\
**Category:** Ruby\
**Created:** [June 8, 2008, 5:35am UTC](https://www.ruby-forum.com/t/eval-bug/139855 "2008-06-08T05:35:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [June 8, 2008, 5:35am UTC](https://www.ruby-forum.com/t/eval-bug/139855/1 "2008-06-08T05:35:28Z")

</div>

I have this code to do timing tests:

def tm(code); s=Time.now; eval code; Time.now-s end

I use it like this:

tm ‘some code’

I have this function that can take two inputs.  
When I run it with just one input like this:

tm ‘Pn(130)’

I can time it with no problems.

When I use a second input like this,

tm ‘Pn(130, mcps)’

running in irb (1.8.6 and 1.9.0-1) it returns an  
unknown method or variable message for the  
second input.

Is this a bug, or expected behavior?  
Can I work around this?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [June 8, 2008, 7:45am UTC](https://www.ruby-forum.com/t/eval-bug/139855/2 "2008-06-08T07:45:56Z")

</div>

On Jun 7, 11:30 pm, jzakiya [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> second input.
> 
> Is this a bug, or expected behavior?  
> Can I work around this?
> 
> Thanks in advance.

I don’t believe that is a bug. At the time that ‘eval’ is running the  
code, the variable ‘mcps’ is out of scope (assuming it is a  
variable). You may need to look into bindings:  
[http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc](http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc).

Let me know if I’m way off base. I do hope that helps.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [June 8, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/eval-bug/139855/3 "2008-06-08T18:14:06Z")

</div>

I think a more “ruby” implementation of tm would be…

def tm  
start = Time.now  
yield  
return Time.now - start  
end

duration = tm {

# any code you want to time goes here

}

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [June 9, 2008, 2:09am UTC](https://www.ruby-forum.com/t/eval-bug/139855/4 "2008-06-09T02:09:33Z")

</div>

On Mon, Jun 09, 2008, Mike C. wrote:

> of course… it’s very “unruby” to say return, so…

I don’t think this is true at all. There are certainly people who  
prefer the implicit return, but likewise there are people who do not.  
I’m in the latter camp, as should be obvious 🙂

Ben

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [June 10, 2008, 12:46am UTC](https://www.ruby-forum.com/t/eval-bug/139855/5 "2008-06-10T00:46:38Z")

</div>

On Jun 8, 3:22 pm, Mike C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def tm; s=Time.now;yield;Time.now-s end
> 
> > return Time.now - start
> 
> > > Thanks in advance.
> 
> Mike C.  
> removed\_email\_address@domain.invalid://www.cargal.net

Thanks, this works great. I learned something. 😉

It sort of acts like a lamda.

But is that still a bug in eval?

Jabari

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [June 8, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/eval-bug/139855/6 "2008-06-08T21:24:38Z")

</div>

of course… it’s very “unruby” to say return, so…

def tm  
start = Time.now  
yield  
Time.now - start  
end

or (if you seem to prefer the one line syntax…

def tm; s=Time.now;yield;Time.now-s end

On Jun 8, 2008, at 12:10 PM, Mike C. wrote:

> }
> 
> > I have this function that can take two inputs.  
> > running in irb (1.8.6 and 1.9.0-1) it returns an  
> > unknown method or variable message for the  
> > second input.
> > 
> > Is this a bug, or expected behavior?  
> > Can I work around this?
> > 
> > Thanks in advance.

Mike C.  
removed\_email\_address@domain.invalid  
[http://www.cargal.net](http://www.cargal.net)

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [July 9, 2008, 6:53am UTC](https://www.ruby-forum.com/t/eval-bug/139855/7 "2008-07-09T06:53:18Z")

</div>

> But is that still a bug in eval?
> 
> Jabari

My question is why does  
eval(“b = 6”)  
print b

err and

b = nil  
eval(“b = 6”)  
print b

succeed? Eval has its own sub-scope?

Note also that  
eval(“b = 6”, binding)  
print b

also fails.

Is this intended, or just a side-effect of the ‘parse then execute’  
stratagem?

Also as an interesting note,  
if you have  
code1  
eval(“code”)  
code2

with 1.8.6, It appears to ‘reparse’ code2 after the eval has completed.  
I assume to make sure that it has accommodated for new variables or  
something. Except that you actually can’t add new local variables, so  
it must be something else.

Thanks.  
-R
