This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
If we want to mix in behavior to a class, we have to use the include method. Unsurprisingly, based on the name, that will include all of the methods in the module in the class that weβre working with. In order to make our modules actually do something, it needs to be included in a class.
Code Samples
Here is our Fetcher
module:
module Fetcher
def fetch(item)
puts "I'll bring that #{item} right back!"
end
end
Once written, we can include that in the Dog
class:
class Dog
include Fetcher
attr_reader :name
def initialize(name)
@name = name
end
end
dog = Dog.new("Fido")
dog.fetch("ball")
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated 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