开发者

strange inheritance in ruby mixins

开发者 https://www.devze.com 2023-01-08 12:30 出处:网络
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)?

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 Fo开发者_如何学Co

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 main object.

You could test this by modifying your bar method to the following

  def bar
    print "InBar class: #{self.class} value: #{self}\n"
  end

And then adding the following 2 lines at the end

2.bar
Fixnum.bar

This would give you the following output

There is no Bar.bar
InBar class: Object value: main
InBar class: Class value: Bar
InBar class: Bar value: #<Bar:0x21ecec>
InBar class: Fixnum value: 2
InBar class: Class value: Fixnum


an include at top-level mixes the module into Object. In so far as it's mixed into Object it's available as an instance method on everything.

0

精彩评论

暂无评论...
验证码 换一张
取 消