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 Being Included

Why the "attr_accessor :fetch_count" statement has been written into the self.included block?

I noticed that in the Fetcher module, the "attr_accessor :fetch_count" statement has been written into the self.included block.

There is a specific reason for doing that?

2 Answers

Andrei Burichita
Andrei Burichita
9,989 Points

i am way to late for this but: as mentioned in the video... the attr_accessor :fetch_count has been intentionally put in the Fetcher module body because by doing so you write it only once. This is why you do not need to write it in the Dog class anymore or any other class or function....by including Fetcher you automatically include the attr_accessor :fetch_count

Muhammad Saif
Muhammad Saif
11,565 Points

Your answer is not wrong, however not answering the question. The attr_accessor could be written outside the self.included method and also inside that method. His question is why inside?

Donghai Zhang
Donghai Zhang
Front End Web Development Techdegree Student 7,036 Points

I guess the fetch_count variable is within the scope of the module itself. Every time you include the module and invoke the fetch method, the fetch_count will be incremented by 1 and keep the state until it is changed again.

However I do not know what the point it is to doing so.