我怎样才能做到他们所说的here,但是在 Ruby 中呢?
您将如何在对象上执行该功能?以及你将如何执行全局函数(请参阅 jetxee 的answer在提到的帖子上)?
示例代码:
event_name = "load"
def load()
puts "load() function was executed."
end
def row_changed()
puts "row_changed() function was executed."
end
#something here to see that event_name = "load" and run load()
UPDATE:如何获取全局方法?或者我的全局函数?
我尝试了这条附加线
puts methods
以及未列出的 load 和 row_change。
直接在对象上调用函数
a = [2, 2, 3]
a.send("length")
# or
a.public_send("length")
如预期返回 3
或对于模块功能
FileUtils.send('pwd')
# or
FileUtils.public_send(:pwd)
和本地定义的方法
def load()
puts "load() function was executed."
end
send('load')
# or
public_send('load')
文档:
- Object#public_send
- Object#send
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)