Reputation: 838
Why does the code below return NoMethodFound
error:
module Mod1
def print_string
puts 'hello ruby'
end
end
module Mod2
include Mod1
end
Mod2.print_string
while the code below runs fine?
module Mod1
def print_string
puts 'hello ruby'
end
end
module Mod2
extend Mod1
end
Mod2.print_string
Upvotes: 0
Views: 871
Reputation: 36880
Yes, extend
adds the module's methods as class methods to the caller, whereas include
adds them as instance methods.
An example (using classes instead of modules since modules can't have instances...)
This works...
module Mod1
def print_string
puts 'hello ruby'
end
end
class Mod2
extend Mod1
end
Mod2.print_string
And this works...
module Mod1
def print_string
puts 'hello ruby'
end
end
class Mod2
include Mod1
end
mod_instance = Mod2.new
mod_instance.print_string
Upvotes: 0
Reputation: 3721
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.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 is also a fantastic resource, because they document the functionality precisely.
ref: John
Upvotes: 1