Ruby: difference between __method__ and __callee__

Just yesterday I learned a little something in Ruby that I found interesting. It’s the difference between __method__ and __callee__.

Both are supposed to return the name of the current method, but there’s a subtle difference:

I put together an example that illustrates the difference:

 1class Bar
 2  def foo
 3    puts "__callee__ is #{__callee__}"
 4    puts "__method__ is #{__method__}"
 5  end
 6  alias :baz :foo
 9bar = # => Prints "foo" and "foo"
11bar.baz # => Prints "baz" and "foo"

This illustrates very well the difference between “message” and “method”. One is the name we use to invoke a method. The other is the name of the method ultimately called.