I was looking at the ruby mixin blog post, and it says that when a module is included in a class its self.included()
method is called.
My question is, where is this behaviour officially documented? I can't seem to locate it on the ruby-docs.org website or the pickaxe.
The module methods & instance methods can be accessed by both extend & include keyword. Regardless of which keyword you use extend or include : the only way to access the module methods is by Module_name::method_name.
As with class methods, you call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.
A Module is a collection of methods, constants, and class variables. Modules are defined as a class, but with the module keyword not with class keyword. Important Points about Modules: You cannot inherit modules or you can't create a subclass of a module. Objects cannot be created from a module.
Modules are about providing methods that you can use across multiple classes - think about them as "libraries" (as you would see in a Rails app). Classes are about objects; modules are about functions. For example, authentication and authorization systems are good examples of modules.
While it's not on Ruby Doc for some reason, included
actually is documented. Running ri Module.included
in the terminal provides this:
included( othermod )
Callback invoked whenever the receiver is included in another module or class. This should be used in preference to
Module.append_features
if your code wants to perform some action when a module is included in another.module A def A.included(mod) puts "#{self} included in #{mod}" end end module Enumerable include A end
This documentation can be found in the Ruby source in object.c
. Sadly, Module.extended
is not documented.
I suspect it's not on the RubyDoc website because it's a private method, and private methods aren't currently displayed.
People are aware of this issue, but they haven't yet worked out how to handle methods that are private even though they aren't implementation details.
I've created a bug report at http://bugs.ruby-lang.org/issues/6381
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With