开发者

Create a ruby Proc from a string

开发者 https://www.devze.com 2022-12-23 17:35 出处:网络
I want to define the block as a string, then create the lambda. The following example does not work.

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


This works

eval  "lambda { " + code_string + " }"

I just don't know why this one does and the other does not.


eval "lambda {#{code_string}}"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号