Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the ruby module self.included and self.extended behaviour documented?

Tags:

ruby

mixins

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.

like image 424
Zeeshan Qureshi Avatar asked Apr 29 '12 05:04

Zeeshan Qureshi


People also ask

How do you access a module method in Ruby?

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.

How do you call a module method in Ruby?

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.

What are modules Ruby?

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.

What is the difference between modules and classes in Ruby?

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.


2 Answers

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.

like image 141
Andrew Marshall Avatar answered Nov 04 '22 18:11

Andrew Marshall


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

like image 33
Andrew Grimm Avatar answered Nov 04 '22 18:11

Andrew Grimm