Read more

Ruby: What extend and include do

Dominik Schöler
April 10, 2013Software engineer at makandra GmbH

All Rubyists should be familiar with the common definitions for include and extend. You include a module to add instance methods to a class and extend to add class methods. Unfortunately, this common definition isn’t entirely accurate. It fails to explain why we use instance.extend(Module) to add methods to an instance. Shouldn’t it be instance.include(Module)? To figure this out we’re going to start by discussing where methods are stored.

  • include: Adds methods from the provided Module to the object
  • extend: Calls include on the singleton class of the object

Further reading

After you understood include and extend you might want to look into more Ruby metaprogramming basics:

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot
Posted by Dominik Schöler to makandra dev (2013-04-10 09:01)