eval bug?

J

jzakiya

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

miles.sterrett

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.

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.

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

Mike Cargal

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
}
 
M

Mike Cargal

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

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
}

Mike Cargal
(e-mail address removed)
http://www.cargal.net
 
B

Ben Bleything

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
 
J

jzakiya

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





Mike Cargal
(e-mail address removed)://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
 
R

Roger Pack

But is that still a bug in eval?

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
 

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

Similar Threads

Maybe a bug in JavaScript? 2
I'm about to get in trouble with the HTML <body></body> tags 10
Avoiding eval 5
Problem with code 4
possibly bulletproof eval() 14
eval 10
Uhhhhh, What can I do next? 6
Eval Weird Behavior 1

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top