Getting a Reference to a Method : Method Reflection « Reflection « Ruby






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

 








Related examples in the same category

1.query almost any object within Ruby for the methods that are defined within it
2.Sort all method from Object
3.Listing Methods Unique to an Object
4.Method pointer