Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.
So is the major difference just this or is a bigger dragon lurking? e.g.
module ReusableModule def module_method puts "Module Method: Hi there!" end end class ClassThatIncludes include ReusableModule end class ClassThatExtends extend ReusableModule end puts "Include" ClassThatIncludes.new.module_method # "Module Method: Hi there!" puts "Extend" ClassThatExtends.module_method # "Module Method: Hi there!"
include? is a String class method in Ruby which is used to return true if the given string contains the given string or character. Syntax: str. include? Parameters: Here, str is the given string. Returns: true if the given string contains the given string or character otherwise false.
The Ruby include statement simply makes a reference to a named module. If that module is in a separate file, you must use require to drag that file in before using include . Second, a Ruby include does not simply copy the module's instance methods into the class.
What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).
Ruby allows you to create a class tied to a particular object. In the following example, we create two String objects. We then associate an anonymous class with one of them, overriding one of the methods in the object's base class and adding a new method.
extend - adds the specified module's methods and constants to the target's metaclass (i.e. the singleton class) e.g.
Klazz.extend(Mod)
, now Klazz has Mod's methods (as class methods)obj.extend(Mod)
, now obj has Mod's methods (as instance methods), but no other instance of of obj.class
has those methods added.extend
is a public methodinclude - By default, it mixes in the specified module's methods as instance methods in the target module/class. e.g.
class Klazz; include Mod; end;
, now all instances of Klazz have access to Mod's methods (as instance methods)include
is a private method, because it's intended to be called from within the container class/module.However, modules very often override include
's behavior by monkey-patching the included
method. This is very prominent in legacy Rails code. more details from Yehuda Katz.
Further details about include
, with its default behavior, assuming you've run the following code
class Klazz include Mod end
@@foo
or @@bar
super
in Klazz#foo will check for Mod#foo before checking to Klazz's real superclass's foo method. See the RubySpec for details.).Of course, the ruby core documentation is always the best place to go for these things. The RubySpec project was also a fantastic resource, because they documented the functionality precisely.
#include
RubySpec rubydoc #included
RubySpec rubydoc #extend
RubySpec rubydoc #extended
RubySpec rubydoc #extend_object
RubySpec rubydoc #append_features
RubySpec rubydoc 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