views:

851

answers:

4

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.

+1  A: 

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
Sorry. I'll copy some of the wording and translate to make it Ruby specific. +1
tyndall
+9  A: 

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')
Colin Gravill
+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.
tyndall
I was curious to what it was doing under the hood.
tyndall
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
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?
tyndall
Good on you for looking stuff up in the source! :)
Colin Gravill
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
@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
@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
+3  A: 

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
+1. like this. Ruby has such great syntax. Love it.
tyndall
+1  A: 

I have covered several ways for Ruby's dynamic method calling in the past. Have a look at them.

khelll