1

Following the advice here I wrote some specs that use Object#method to test that a method was properly aliased. In some cases the class is aliasing a method the comes from an included module. This works for me in 2.2 but not in 2.3 and 2.4

I did some experimentation and the difference can be boiled down to this:

module Foo
  def foo
  end
  alias_method :foo_foo, :foo
end

class Bar
  include Foo
  alias_method :bar, :foo
end

bar = Bar.new
bar.method(:foo) == bar.method(:bar)
# true in Ruby 2.2
# false in Ruby 2.3+

bar.method(:foo) == bar.method(:foo_foo)
# true in Ruby 2.2+

Does anyone know if this was an intentional change in Ruby 2.3? If so, what's going on here?

I specifically tested 2.2.3, 2.2.7, 2.3.0, 2.3.3, 2.3.4, and 2.4.1.

Jeff Manian
  • 81
  • 1
  • 4
  • _Sidenote:_ [`Method#original_name`](https://ruby-doc.org/core/Method.html#method-i-original_name) seems to be a better fit to achieve the result. – Aleksei Matiushkin Aug 10 '17 at 17:26

1 Answers1

0

It turns out this was an intentional change in Ruby 2.3.

The reason is that Method#== returns false if the owners are different. From the bug discussion:

If owners of methods are different, the behavior of super is different in the methods, so Method#== should not return true for the methods.

As mudasobwa pointed out, Method#original_name is a good alternative for this situation. Method#source_location seems like another robust option.

Jeff Manian
  • 81
  • 1
  • 4