Why does this eval not work in Ruby

Posted by Anil on Stack Overflow See other posts from Stack Overflow or by Anil
Published on 2012-06-11T04:25:00Z Indexed on 2012/06/11 4:40 UTC
Read the original article Hit count: 150

Filed under:

Can you explain this?

I want to eval values and calculations from two different sources. One source gives me the following info(programmatically):

'a = 2'

The second source gives me this expression to evaluate:

'a + 3'

This works:

a = 2
eval 'a + 3'

This also works:

eval 'a = 2; a + 3'

But what I really need is this, and it doesn't work:

eval 'a = 2'
eval 'a + 3'

I would like to understand the difference, and how can I make the last option work.

Thanks for your help.

© Stack Overflow or respective owner

Related posts about ruby