Jan 252010

There are three ways to invoke a method. Most of the time you’ll probably only need #1, but #2 and #3 are used when you are doing something called metaprogramming – calling methods based on dynamic information.

1. object = Object.new
puts x.some_method
#=> 282660

2. puts x.send(:some_method)
#=> 282660

3. puts x.method(:some_method).call
#=> 282660

Extending this concept a little further is the try method,
which is like send but will do a nil-check on the method first to make sure it is there (and not complain if it is not).

 Posted by at 12:41 pm

 Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.