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

D

Dirk Traulsen

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top