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

MICHAEL P
MICHAEL P
5,191 Points

Code That Jason wrote in video does not work! Please help!

Hi, The code that Jason wrote in the video does not work! Please help!

Here is the following code that was written in the video. I followed along exactly, doing the steps that Jason does, and it does not work. I would appreciate help!

module Fetcher def self.included(klass) puts "#{klass} has been included" attr_accessor: fetch_count end

def fetch(item) @fetch_count || = 0 @fetch_count += 1 puts "[#{@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")

I get the following errors when running the code, and I do not know how to fix them!

included.rb:4 syntax error, expected ':', expecting keyword_end attr_reader: fetch_count

included.rb:8 syntax error, unexpected '=' @fetch_count || = 0

2 Answers

Andrei Burichita
Andrei Burichita
9,989 Points

i presume you have found your errors by now, but if anyone would like to see where the error comes from:

attr_accessor: fetch_count ---> attr_accessor :fetch_count . ( ":" should be assigned to "fetch_count")

@fetch_count || = 0 --> @fetch_count ||= 0 ( extra space between the || and = )

should work now.

Jason code works like a charm, here's it

module Fetcher
  def self.included(klass)
    puts "#{klass} has been included"
    attr_accessor :fetch_count
  end
  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")

dog2 = Dog.new("Spot")
dog2.fetch("ball")
dog2.fetch("toy")

check it & compare it with your code, and you will find the mistakes in your code.

Regards,