示例代码:
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()
更新: 你如何获得全局方法?还是我的全局函数?
我尝试了这条附加线
puts methods
并在未列出的地方加载和行更改。
直接在对象上调用函数
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