Why does include behave differently at the top level?

I used the following hook to check the module that makes the inclusion when I do include Foo:

module Foo
  def self.included(includer)
    puts includer
  end
end

Module#includebehaves differently in the module (where it is commonly used) against the top level. Inside a module selfis a module that is an instance Module. When I invoke the includemodule that performs the inclusion , thatself :

module Bar
  puts self   # => Bar
  include Foo # => includer: Bar
end

At the top level of the ruby ​​script, selfthere is main, which is an instance Object. When I call includeat the top level, the module that performs the inclusion,, Objectis a class of thatself :

puts self    # => main
include Foo  # => includer: Object

Can someone explain why?

; to_s inspect , main, Object.new to_s inspect , : #<Object:0x007fae0a87ac48>.

+4

Source: https://habr.com/ru/post/1610196/


All Articles