Ruby - 将变量传递给 eval 方法
我有一个引用方法的变量,我使用 eval 关键字调用该方法,
a_test = "myvariable"
eval a_test
def myvariable
(...)
end
我想将变量传递给方法,例如
def myvariable(var1)
(...)
end
是否有人熟悉实现此目的的任何“习惯用法”方式。做类似的事情
eval a_test "string_test"
自然会失败,因为解释器将查找名为“a_test”的函数
I have a variable which reference a method, I call the method with the eval keyword
a_test = "myvariable"
eval a_test
def myvariable
(...)
end
I would like to pass a variable to method, such as
def myvariable(var1)
(...)
end
Is anyone familiar with any "idiom" way of accomplishing this. Doing something like
eval a_test "string_test"
will naturally fail since a lookup will be done by the interpreter for a function called "a_test"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该对你有用
,但在 ruby 中,做这样的事情会更合适
阅读更多关于
发送
This should work for you
In ruby though, it would be more appropriate to do something like this
Read more about
send