Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Modules Include and Extend Extend

Isis van der Plas
Isis van der Plas
13,445 Points

what is the difference between include and extend?

could someone try to explain me in "baby language" (english is not my native language) what the difference between including and extending a module is? I think Jason explained it like this:

So by calling extend and passing in the module name, 6:00 instead of being mixed in to instances of the class, 6:04 extend mixes in to the class methods of the class. 6:11 It can be a little bit confusing. 6:13 Go ahead and try it on your own now using WorkSpaces.

so I guess basically, the question is, what is the difference between instances and methods of the class? I can't seem to wrap my head around it, or will this get clearer as we go along?

2 Answers

Vladislav Trotsenko
Vladislav Trotsenko
9,291 Points

Hello, Isis! include provides instance methods for the class that mixes it in. extend provides class methods for the class that mixes it in. I have demonstrated how you can use class methods and mix it in the case below.

class SomeClass
extend SomeModule #If you want to mix class methods use 'extend'
@@class_variable = "I'm class var" #@@class_variable - area of visibility will be the class and all it's instances
  def self.class_method #pay attention, class methods starts from self

SomeClass.class_method #=> "I'm class var"

Cheers! P.S.: sorry for my 'baby-english', it's not my native language too :)

Enrica Fedeli-Jaques
Enrica Fedeli-Jaques
6,773 Points

probably late for you but useful to others coming here after me, I found this article helpful: https://www.geeksforgeeks.org/include-v-s-extend-in-ruby/ i think jason's video on this is not very clear :(