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 trialMICHAEL P
5,191 PointsMy Ruby Code is not working! 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
Kevin Elliott
15,653 PointsHi Michael,
The code did not format well when you pasted it into the post, so it is difficult to know if the layout is due to your own typing or due to the paste. You might want to click "Markdown Cheatsheet" below in order to learn about the syntax you can use when creating posts and comments here that allow you to stylize portions of your content. For example, you can mark it with backticks to specify that you have code.
Like this:
def method
puts 'hi i am formatted!'
end
Back to your question.
The error indicates that there is a problem where a colon :
is expected. It looks to me like you have:
attr_accessor: fetch_count
when it should be:
attr_accessor :fetch_count
Notice the subtle difference. A colon in front of a word creates a Symbol
(such as :fetch_count
), while a word without the colon is a variable (such as fetch_count
). In this case the runtime compiler is expecting a symbol after attr_accessor
, which by the way is a method that creates accessors for an attribute.
To make things more confusing for you, when using a Hash
, colons after a word can also be a Symbol
as they are keys to the Hash
. For example:
{ something: :hi }
In this case both something
and :hi
are symbols, since the newer Hash
syntax allows for the removal of "hash rockets" (:something => :hi
) to simplify the syntax.
I really hope to not confuse you here with all of this. In your case, I do believe the colon after attr_accessor
is in the wrong place, so fix it by one character position and you should resolve this error.
MICHAEL P
5,191 PointsThank you