且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Ruby中的函数名称从字符串中调用函数

更新时间:2023-11-15 13:49:52

直接在对象上调用函数

  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')