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

Can anyone explain self.included ?

I dont really get from the video what the self.included method is doing? I am able to use all the methods from the module before i write the method ?

1 Answer

The self.included method gets called whenever a module is included in another class, using the 'include' statement. If your module needs to be initialized somehow before it can be used, this a great place to put that code.

Here's an example:

module Apple
  def self.included(class_name)
    puts "Hi, I've been included by the #{class_name} class!"
  end
end

class Banana
  include Apple
end

As long as I didn't make any typos, this otherwise useless code should output "Hi, I've been included by the Banana class!"