gpt4 book ai didi

ruby - 你能在 Ruby 调用者的上下文中评估代码吗?

转载 作者:数据小太阳 更新时间:2023-10-29 06:29:01 24 4
gpt4 key购买 nike

本质上我想知道是否 the following可以在 Ruby 中完成。

例如:

def bar(symbol) 
# magic code goes here, it outputs "a = 100"
end

def foo
a = 100
bar(:a)
end

最佳答案

您必须将 foo 的上下文传递给 bar:

def foo
a = 100
bar(:a, binding)
end
def bar(sym, b)
puts "#{sym} is #{eval(sym.to_s, b)}"
end

关于ruby - 你能在 Ruby 调用者的上下文中评估代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1356749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com