Ruby provide with the following ways by which you can invoke the method based on your requirement :
s= "ruby method"
1) Call a public instance method directly
p s.length #=> 11
2) Other way is to invoke a method dynamically in ruby is to send a message to the object :
p s.send(:length) #=> 11
3) Other way is to instantiate a method object and then call it:
object = s.method(:length)
p object.call #=> 11
4) And the last way is to use the eval method:
eval "s.length" #=> 11
Well instantiating a method object is the fastest dynamic way of calling a method and eval is the slowest.
0 Comment(s)