Method: Module#home
- Defined in:
- lib/core/facets/module/home.rb
#home ⇒ Object
Returns the module or class containing the receiver.
module ::HomeExample
module M
module N
end
end
end
HomeExample::M::N.home #=> HomeExample::M
The home of a top-level module/class is Object.
HomeExample.home #=> Object
This method is called home because techinally a module or class is just a constant reference, and as such can reside with multiple namespaces, like any variable. For example:
module OtherPlace
M = ::HomeExample::M
end
In this example, you might think that OtherPlace::M‘s home would be OtherPlace, but OtherPlace::M is the same object as HomeExample::M, and it can have only one “home” –the original location of it’s definition.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/core/facets/module/home.rb', line 29 def home #homename = /::[^:]+\Z/ =~ name ? $` : nil if homename homename.split(/::/).inject(self) do |mod, cref| if /\:(0x.*?)\>$/ =~ cref # TODO: does this ever happen? #p $1.to_i(16) ObjectSpace._idref($1.to_i(16)) else mod.const_get(cref) end end else Object end end |