Reputation: 168081
An instance method defined on a module:
module A
def foo; :bar end
end
seems to be able to be called as a module method of that module when that module is included:
include A
A.foo # => :bar
Why is that?
Upvotes: 3
Views: 126
Reputation: 7530
You're including A into Object.
module A
def self.included(base)
puts base.inspect #Object
end
def foo
:bar
end
end
include A
puts A.foo # :bar
puts 2.foo # :bar
#puts BasicObject.new.foo #this will fail
Also note that the top level object main
is special; it's both an instance of Object and a sort of delegator to Object.
See http://banisterfiend.wordpress.com/2010/11/23/what-is-the-ruby-top-level/
Upvotes: 6
Reputation: 1198
Tried this out in irb, it included it in Object
. include A
also returns Object
irb > module A
irb > def foo; :bar end
irb > end
=> nil
irb > Object.methods.include? :foo
=> false
irb > include A
=> Object
irb > Object.methods.include? :foo
=> true
Upvotes: 0