183

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.

4 Answers 4

270

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:

Sign up to request clarification or add additional context in comments.

11 Comments

+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.
I was curious to what it was doing under the hood.
It's defined on object - ruby-doc.org/core/classes/Object.html#M000332 I picked a random function for interest value.
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?
Good on you for looking stuff up in the source! :)
|
45

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.

4 Comments

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
if you want to send some argument with send call use something like this ClassName.send("method_name", arg1, arg2)
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.
Blog post link is dead, btw.
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.

2 Comments

meth = a.method? isn't this call the function already? what if the method returns something?
FYI for when the method doesn't exist: "my_string".method('blah') #=> NameError: undefined method blah' for class String'
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.

1 Comment

Sorry. I'll copy some of the wording and translate to make it Ruby specific. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.