Project

General

Profile

Actions

Bug #9443

closed

Prepended methods are skipped if calling alias

Added by bcardarella (Brian Cardarella) almost 11 years ago. Updated over 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:60031]

Description

I am unsure if this is related to https://bugs.ruby-lang.org/issues/7842 as that issue specifically mentions the user of "super" which my example code does not use. Anyway, here is the test to recreate:

require 'test/unit'

module A
  def foo
    'foo2'
  end
end

class B
  def foo
    'foo1'
  end
  alias_method :bar, :foo
end

B.send(:prepend, A)

class AliasTest < Test::Unit::TestCase
  def test_no_alias
    assert_equal 'foo2', B.new.foo
  end

  def test_with_alias
    assert_equal 'foo2', B.new.bar
  end
end

test_with_alias fails and will only call the original "foo" method.

I have confirmed this on Ruby 2.0.0-p353 & 2.1.0

Updated by nagachika (Tomoyuki Chikanaga) over 10 years ago

I think it's intended behavior. nobu, how do you think?

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Description updated (diff)
  • Status changed from Open to Rejected

It's a spec.
Aliased methods share the body, so alias_method_chain can work.
It's not related to prepend.

Updated by trans (Thomas Sawyer) over 10 years ago

alias in Ruby really means "copy method". It would be nice if there were a way to truly alias, such that the two names refer to same definition.

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

alias in Ruby does refer to the same definition with the original method.
Probably what you want would be Forwardable.

Updated by duerst (Martin Dürst) over 10 years ago

  • Subject changed from Pepended methods are skipped if calling alias to Prepended methods are skipped if calling alias
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0