Module Tidbits

Posted by Lee Marlow Thu, 16 Nov 2006 11:35:06 GMT

I always forget what including versus extending a Module, so here's a little reminder for myself.
module UsefulMethods
  def some_cool_thing
    1 + 1 == 0
  end
end

class IncludeUsefulInstanceMethods
  include UsefulMethods
end

class IncludeUsefullClassMethods
  extend UsefulMethods
end

puts IncludeUsefulInstanceMethods.new.some_cool_thing # => false
puts IncludeUsefullClassMethods.some_cool_thing # => false
So, including a module basically shoves the module's methods into the class as instance methods, while extending a module puts them in as class methods. That's it. By the way, this was inspired by Dave Thomas' metaprogramming talk at the Rails Edge Studio in Denver.
Comments

Leave a response

Comments