Mega Code Archive

 
Categories / Ruby / Reflection
 

Getting a Reference to a Method

s = 'A string' length_method = s.method('length') # => #<Method: String#length> length_method.arity                # => 0 length_method.call                 # => 8 p 1.method(:succ).call                # => 2 p 5.method('+').call(10)              # => 15 p [1,2,3].method(:each).call { |x| puts x } # 1 # 2 # 3