我在玩线程和评估时遇到了问题。我的代码是:
thread = Thread.start {
$SAFE = 4;
eval("`touch ~/test`")
}
thread.join
但它会导致错误:
ArgumentError: $SAFE=4 is obsolete
from (irb):2:in `block in irb_binding'
我发现 $SAFE=4
在 Ruby 2.1 中已过时:
$SAFE=4 is obsolete. If $SAFE is set to 4 or larger, an ArgumentError is raised.
但没有提到应该使用什么来代替。有没有等同于 $SAFE=4
的东西?基本上我想要的是以最安全的方式运行 eval
。
最佳答案
我能看到的唯一方法是利用 Binding 进行安全评估
http://rdoc.info/stdlib/core/2.1.0/Binding
class Demo
def initialize(n)
@secret = n
end
def get_binding
return binding()
end
end
k1 = Demo.new(99)
b1 = k1.get_binding
k2 = Demo.new(-3)
b2 = k2.get_binding
eval("@secret", b1) #=> 99
eval("@secret", b2) #=> -3
eval("@secret") #=> nil
关于ruby - $SAFE=4 等同于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24312654/