Scope difference of alias + alias_method a bug or a feature?

Discussion in 'Ruby' started by Dirk Traulsen, Dec 30, 2007.

  1. Chan Sammy in ruby-talk[284135] stumbled over the different scope of
    alias and alias_method. See his example a little changed:

    puts RUBY_VERSION #=> 1.8.6
    class T
    def self.redefine
    p self
    alias_method :bar_alm, :foo
    alias bar_a foo
    end
    def foo ; puts "T's foo" ; end
    end

    class S < T
    def foo ; puts "S's foo" ; end
    redefine
    end

    s = S.new #=> S
    s.bar_alm #=> S's foo
    s.bar_a #=> T's foo
    p S.instance_methods.grep(/foo|bar/) #=> ["foo", "bar_a", "bar_alm"]
    p T.instance_methods.grep(/foo|bar/) #=> ["foo", "bar_a"]
    __END__

    So:
    alias_method results in an instance method bar_alm in S.
    alias results in an instance method bar_a in T (and so in S).

    I would like to repeat his question to straighten it out:
    Is this difference between alias and alias_method a bug or a feature?

    Thanks
    Dirk
    Dirk Traulsen, Dec 30, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    16
    Views:
    533
    Ilias Lazaridis
    Jun 10, 2006
  2. grocery_stocker
    Replies:
    9
    Views:
    735
    grocery_stocker
    May 24, 2008
  3. Angel Martin

    alias or alias_method problem

    Angel Martin, Apr 29, 2004, in forum: Ruby
    Replies:
    6
    Views:
    116
    Kristof Bastiaensen
    Apr 29, 2004
  4. Chan Sammy
    Replies:
    4
    Views:
    121
    Chan Sammy
    Dec 19, 2007
  5. robic0
    Replies:
    7
    Views:
    100
    Samwyse
    Jan 24, 2006
Loading...

Share This Page