Calling a Method From a String With the Method’s Name in Ruby

To call functions directly on an object

a = [2, 2, 3]
a.send("length")
# or
a.public_send("length")

which returns 3 as expected

or for a module function

FileUtils.send('pwd')
# or
FileUtils.public_send(:pwd)

and a locally defined method

def load()
    puts "load() function was executed."
end

send('load')
# or
public_send('load')

Documentation:

Leave a Comment