本文介绍了使用Ruby中的函数名称从字符串中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么能做他们正在谈论的,但在Ruby?
你如何做一个对象的功能?你将如何做一个全局函数(参见jetxee的) b
event_name =load
def load()
putsload()函数已执行。
end
def row_changed()
putsrow_changed()函数已执行。
end
#s something here to see the event_name =loadand run load()
更新:
您如何获取全局方法?或我的全球功能?
我试过这个额外的行
puts方法
以及load和row_change在哪里未列出。
解决方案
直接在对象上调用函数
a = [2,2,3]
a.send(length)
返回3如预期的那样
或模块函数
$ b $
code> FileUtils.send('pwd')
和本地定义的方法
def load()
putsload()function was executed。
end
send('load')
How can I do what they are talking about here, but in Ruby?
How would you do the function on an object? and how would you do a global function (see jetxee's answer on the post mentioned)?
EXAMPLE CODE:
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:How do you get to the global methods? or my global functions?
I tried this additional line
puts methods
and load and row_change where not listed.
解决方案
To call functions directly on an object
a = [2, 2, 3]
a.send("length")
which returns 3 as expected
or for a module function
FileUtils.send('pwd')
and a locally defined method
def load()
puts "load() function was executed."
end
send('load')
这篇关于使用Ruby中的函数名称从字符串中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!