buaaji
buaaji

Reputation: 121

Why alias_method is an instance method?

Below is the typical usage of alias_method,

class A
def say
    puts 'say'      
end

alias_method :talk, :say
end

alias_method is a private instance method defined in Module class, but in above code, it's more like a class method instead of an instance method, why does the above code work?

Upvotes: 2

Views: 1442

Answers (1)

sawa
sawa

Reputation: 168081

It is a class method of A as you correctly notice, and is at the same time an instance method of Module class, of which the Class class is a subclass. Class methods are not exclusive to instance methods. All class methods are instance methods of the Class class unless they are methods on a singleton class.

In the above example, alias_method is defined on Module class. So, A, which is an instance of Module can be the receiver of a method call to alias_method. But since this method is private, the receiver needs to implicit.

Upvotes: 1

Related Questions