module
http://lizabinante.com/blog/modules-and-self-in-ruby/
Module Mixin
extend vs. include in classes
extend vs. include in classesinclude: instance methodsextend: class methods
module Animals
def mammals
["sloths", "puppies", "porcupines"]
end
end
# - - - - - - - - include
class Zoo
include Animals
end
zoo = Zoo.new
zoo.mammals # => ["sloths", "puppies", "porcupines"]
zoo.methods - Object.methods # => mammals
# - - - - - - - - extend
class PetStore
extend Animals
end
PetStore.mammals # => ["sloths", "puppies", "porcupines"]
pet_store = PetStore.new
pet_store.mammals # => undefined method `mammals' for #<PetStore:0x007f858a919f30> (NoMethodError)
PetStore.methods - Object.methods # => mammals
pet_store = PetStore.new
pet_store.methods - Object.methods # =>Module Methods
extend self:
Not change the behavior of include or extend in your classes
self method name prefix
change the behavior of include or extend in your classes (
selfprefix can not be mixed in)only access some of methods in module (access
selfprefix only)
extend self
extend selfself method name prefix
self method name prefixLast updated
Was this helpful?