Created
March 13, 2012 13:02
-
-
Save granolocks/2028612 to your computer and use it in GitHub Desktop.
Difference between include and extend for loading of a module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Mojule | |
def dynamic_method | |
if self.class.eql?(Class) | |
puts "I am now a class method of #{self.name}" | |
else | |
puts "I am now an instance method of #{self.class}" | |
end | |
end | |
end | |
class Klass | |
# extend Klass with Mojule | |
extend Mojule | |
end | |
class Classs | |
# include Mojule in Classs | |
include Mojule | |
end | |
Klass.dynamic_method # => "I am now a class method of #{self.name}" | |
# Klass.new.dynamic_method | |
# => class_module_via_method.rb:26: undefined method `dynamic_method' for #<Klass:0xb76f4584> (NoMethodError) | |
Classs.new.dynamic_method # => "I am now an instance method of Classs" | |
# Classs.dynamic_method | |
# => class_module_via_method.rb:30: undefined method `dynamic_method' for Classs:Class (NoMethodError) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment