181

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

4 Answers4

267

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:

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Colin Gravill
  • 4,234
  • 1
  • 21
  • 16
  • 4
    +1 That works. This may be a dumb follow up ... but how come I can't find the word send in the Ruby source at - C:\ruby\lib\ruby\1.8\fileutils.rb? Thought I would find the send function in there. – BuddyJoe Sep 10 '09 at 21:15
  • 1
    I was curious to what it was doing under the hood. – BuddyJoe Sep 10 '09 at 21:15
  • It's defined on object - http://www.ruby-doc.org/core/classes/Object.html#M000332 I picked a random function for interest value. – Colin Gravill Sep 10 '09 at 21:44
  • 1
    Interesting because before I read your answer twice, and fully grok'd it I ran the FileUtils.send("load") and it ran my function. so if I understand this correctly by creating functions in "global" am I adding the methods onto the root object? or not? – BuddyJoe Sep 10 '09 at 21:50
  • 13
    Good on you for looking stuff up in the source! :) – Colin Gravill Sep 10 '09 at 21:50
  • I think they are being created as private method on the root object so global method might not be the best name for them – Colin Gravill Sep 10 '09 at 21:59
  • @Tyndall: Maybe it would be helpful not to think of them as functions. The only true functions in Ruby are created like `lambda {|x| x+1}`. Everything else is a method of some object. When you open an empty Ruby file and type `def foo(bar)`, you are defining a private method of the class Object. It will be available everywhere (since everything is an Object), but more specific classes are free to override it as well. – Chuck Sep 10 '09 at 22:09
  • @tyndall, even lambda isn't really a function, it creates a Proc object. There was discussion about this here: http://stackoverflow.com/questions/1933390/getting-ruby-function-object-itself – Tim Snowhite Apr 20 '10 at 14:48
  • What if I need to pass parameters to the `load` function? How to do? – user2503775 Jun 09 '15 at 14:28
  • until (a = gets.chomp) =~ /(?:ex|qu)it/i send(a) end – David Sigley Feb 01 '17 at 11:38
  • Also you can pass an argument object.send(:method, arg) – Carlos Gómez Dec 29 '22 at 15:14
44

Three Ways: send / call / eval - and their Benchmarks

Typical invocation (for reference):

s= "hi man"
s.length #=> 6

Using send

s.send(:length) #=> 6

Using call

method_object = s.method(:length) 
p method_object.call #=> 6

Using eval

eval "s.length" #=> 6

 

Benchmarks

require "benchmark" 
test = "hi man" 
m = test.method(:length) 
n = 100000 
Benchmark.bmbm {|x| 
  x.report("call") { n.times { m.call } } 
  x.report("send") { n.times { test.send(:length) } } 
  x.report("eval") { n.times { eval "test.length" } } 
} 

...as you can see, instantiating a method object is the fastest dynamic way in calling a method, also notice how slow using eval is.

#######################################
#####   The results
#######################################
#Rehearsal ----------------------------------------
#call   0.050000   0.020000   0.070000 (  0.077915)
#send   0.080000   0.000000   0.080000 (  0.086071)
#eval   0.360000   0.040000   0.400000 (  0.405647)
#------------------------------- total: 0.550000sec

#          user     system      total        real
#call   0.050000   0.020000   0.070000 (  0.072041)
#send   0.070000   0.000000   0.070000 (  0.077674)
#eval   0.370000   0.020000   0.390000 (  0.399442)

Credit goes to this blog post which elaborates a bit more on the three methods and also shows how to check if the methods exist.

Community
  • 1
  • 1
cwd
  • 53,018
  • 53
  • 161
  • 198
  • I was just finding this, and I noticed something that wasn't covered. What would I do if I wanted to do `Class.send("classVariable") = 5`? That throws an error. Is there any way around that? The same thing is true for using `Class.method("classVariable").call() = 5` – thesecretmaster Nov 20 '15 at 22:14
  • 2
    if you want to send some argument with send call use something like this ClassName.send("method_name", arg1, arg2) – Aleem Aug 30 '17 at 10:07
  • Shouldn't your benchmark for `call` include instantiating the method object (`m.test.method(:length)`) to accurately represent it's true time? When using `call` you're _likely_ going to instantiate the method object every time. – Joshua Pinter Dec 30 '17 at 22:29
  • Blog post link is dead, btw. – Joshua Pinter Dec 30 '17 at 22:30
34

Use this:

> a = "my_string"
> meth = a.method("size")
> meth.call() # call the size method
=> 9

Simple, right?

As for the global, I think the Ruby way would be to search it using the methods method.

Geo
  • 93,257
  • 117
  • 344
  • 520
3

Personally I would setup a hash to function references and then use the string as an index to the hash. You then call the function reference with it's parameters. This has the advantage of not allowing the wrong string to call something you don't want to call. The other way is to basically eval the string. Do not do this.

PS don't be lazy and actually type out your whole question, instead of linking to something.

dlamblin
  • 43,965
  • 20
  • 101
  • 140