Create a ruby Proc from a string

Posted by user252787 on Stack Overflow See other posts from Stack Overflow or by user252787
Published on 2010-03-17T00:33:47Z Indexed on 2010/03/17 0:41 UTC
Read the original article Hit count: 335

Filed under:
|

I want to define the block as a string, then create the lambda. The following example does not work. Is something like this possible?

code_string = "|x|x*2"

l = lambda {eval(code_string)}

l.call(3) => 6

© Stack Overflow or respective owner

Related posts about ruby

Related posts about lambda