Skip to content

Instantly share code, notes, and snippets.

@granolocks
Created March 13, 2012 13:02
Show Gist options
  • Save granolocks/2028612 to your computer and use it in GitHub Desktop.
Save granolocks/2028612 to your computer and use it in GitHub Desktop.
Difference between include and extend for loading of a module.
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