Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
One thing that’s really neat about the include method when using a module is that it gives us a way to hook in to that behavior and do something when a module is included in a class. We can use this to further extend the functionality in our modules. For example, when a module is included in a class, we could use that to run methods, write methods, or include other modules.
Code Samples
First, we modify our Fetcher
module to have an included
class method. This method defines an attribute on the class it is included in called fetch_count
.
module Fetcher
def self.included(klass)
attr_accessor :fetch_count
end
The fetch
method is then modified to say how many times the fetch count has occurred. The ||=
operator is called the conditional assignment operator. It conditionally sets the value of @fetch_count
to 0 if we haven't accessed it yet.
def fetch(item)
@fetch_count ||= 0
@fetch_count += 1
puts "[#{@name}, Fetch count: #{@fetch_count}] I'll bring that #{item} right back!"
end
end
class Dog
include Fetcher
def initialize(name)
@name = name
end
end
dog = Dog.new("Fido")
dog.fetch("ball")
dog.fetch("toy")
Now, when we create a new Dog
instance and run the fetch
method, we get the following output:
[Spot, Fetch count: 1] I'll bring that ball right back!
[Spot, Fetch count: 2] I'll bring that toy right back!
This fetch_count
variable is specific to each Dog
instance:
dog2 = Dog.new("Spot")
dog2.fetch("ball")
dog2.fetch("toy")
So running that produces the following output:
[Fido, Fetch count: 1] I'll bring that ball right back!
[Fido, Fetch count: 2] I'll bring that toy right back!
[Spot, Fetch count: 1] I'll bring that ball right back!
[Spot, Fetch count: 2] I'll bring that toy right back!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
gene c
13,630 PointsWhy the fetch count needs to have '@' in front when we already defined it in attr_accessor :fetch_count?
Posted by gene cgene c
13,630 Points1 Answer
-
MICHAEL P
5,191 PointsCode That Jason wrote in video does not work! Please help!
Posted by MICHAEL PMICHAEL P
5,191 Points2 Answers
-
Unsubscribed User
11,042 Points0 Answers
-
Unsubscribed User
11,042 Points2 Answers
-
Deepak Rohan Sekar
9,634 Points5 Answers
-
elk6
22,916 Points2 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up