25

for instance in python it is possible to assign a method to a variable:

class MyClass 
    def myMethod(self):
        return "Hi"


 x = MyClass()
 method = x.myMethod

 print method() # prints Hi

I know this should be possible in Ruby, but I don't know what's the syntax.

2 Answers 2

29

You need to grab the method by using method with the method’s name as an argument. This will return you an instance of type Method, which can be called with call().

class MyClass
  def myMethod
    "Hi"
  end
end

x = MyClass.new
m = x.method(:myMethod)
# => #<Method: MyClass#myMethod>

puts m.call
# You can also do m[] instead of m.call()

Note that any arguments would need to be added to the call method.


In many practical cases, however, there is no need to have the method itself saved to a variable in Ruby; if you just want to dynamically call a method (i.e. send a message to an object) and there is no need to save the method, you could also use the send (or __send__ method in case of name clashes).

x = MyClass.new
puts x.send :myMethod # also possible with a string: m.send "myMethod"
# "Hi"

Any arguments should follow the method name:

puts x.send(:myMethod, arg1, arg2)

To use it like this is probably more Ruby-like, as the concept of Method classes is not as prominent as it is in Python. In Python, you can always think of a two step mechanism when doing something like a_string.split(); first you grab the method with a_string.split and then you call it (either implicitly with () or explicitly with __call__()). So, cutting that two-step mechanism is rather natural to do.

Ruby is more based on message passing and to actually get a method class in Ruby, you’ll have to do some more work, because in some way, the method object will have to be constructed for you at that point. So, unless you really need some Methods object in Ruby, you should rather stick to the message passing abstraction and simply use send.

3
  • Good answer. But shouldn't it be x.send rather than m.send?
    – FMc
    Apr 20, 2010 at 23:04
  • Yes, you’re correct. m wouldn’t even be defined in that scenario.
    – Debilski
    Apr 20, 2010 at 23:54
  • Note that method() will return a new object with each invocation -- not something important most of the time, but sometimes a gotcha e.g. in tests. Mar 28, 2016 at 18:09
4

I think you are looking for Proc or lambda block

x = Proc.new { return "Hello World" }
puts x.call

x = lambda { return "Hello World" }
puts x.call

I would read this short post - there is a slight but significant difference in the way the methods behave http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby

2
  • Nope, not really, that's what I found in most sites in the web, but not really what I was looking for, thank anyway.
    – OscarRyz
    Apr 20, 2010 at 19:11
  • 1
    Functional way of doing things! I prefer this answer
    – metrix
    Dec 1, 2014 at 22:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.