I am wondering, why is an included module’s methods mixed in to any subsequent class definitions (as if the class included it in itself)?
module Foo
def bar
print "#{self}\n"
end
end
class Bar
end
begin
Bar.bar
rescue NoMethodError
puts "There is no Bar.bar\n"
end
include Foo
bar
Bar.bar
Bar.new.bar
prints:
There is no Bar.bar main Bar #<Bar:0xb73f2048>
Is this the expected behavior? Why?
When you include Foo in your program but outside of any class or method then it is included in to the current scope which is the
mainobject.You could test this by modifying your bar method to the following
And then adding the following 2 lines at the end
This would give you the following output