0

I have a Ruby method which is aliased to several alternative names, and within the method, I would like to know which alias has called me.

This posting suggests that I could use __callee__ to get at this information. However, this does not work in my case, since I am in a module and the method is a module method. The following example demonstrates the problem:

module Demo
  extend self
  def f
    puts __callee__
  end
  alias g f
  alias_method :h,:f
end

Demo.f
Demo.g
Demo.h

This outputs

f
f
f

and not

f
g
h

Could someone explain, why it doesn't work, and how I can do it correctly?

UPDATE: From the feedback I got here and also from other developers, this seems to be a bug in Ruby, which was fixed in 2.3.4.

5
  • 1
    Works for me (ruby 2.3.4) Jun 16, 2017 at 7:30
  • I tried it with MRI Ruby 2.3.3 for Cygwin, and with JRuby 1.7.26 (language compatible to 1.9.3), and both did not work. There must be something really odd going on; I would be surprised, if this were a long-standing bug, which just got corrected in 2.3.4. You really used copy and paste of my code to run it on your platform? Jun 16, 2017 at 7:34
  • yep, I copy/pasted. Try 2.3.4 and see if it works. I'm on macos, btw. Jun 16, 2017 at 7:35
  • 1
    Indeed, tried in 2.3.3 and I get f f f. So yes, it appears to be fixed in 2.3.4 Jun 16, 2017 at 7:38
  • 2
    bugs.ruby-lang.org/issues/11964
    – Stefan
    Jun 16, 2017 at 9:10

0

Browse other questions tagged or ask your own question.