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 trialmelissaorgill
Front End Web Development Techdegree Student 11,136 PointsClarification on classes
Hi! I'm working on the building a monster class tutorial. Here's a snippet of code. Just wondering why we use the colon ex actions[:scares]+=1 throughout the program.
Thanks1
class Monster
attr_reader :name, :actions
def initialize(name)
@name=name
@actions ={
screams:0,
scares:0,
hides:0,
runs:0
}
end
def say(&block)
print "#{name} says ...."
yield
end
def print_scoreboard
puts "-------------------------"
puts "#{name} scoreboard"
puts"---------------------------"
puts" -Screams: #{actions[:screams]}"
puts " -Scares: #{actions[:scares]}"
puts "-Runs: #{actions[:runs]}"
puts "-Hides:#{actions[:hides]}"
puts"---------------------------"
end
def scream (&block)
actions[:screams]+=1
print "#{name} screams!"
yield
end
def scare (&block)
actions[:scares]+=1
print"#{name} scares you!"
yield
end
def run (&block)
actions[:runs]+=1
print"#{name} runs!"
yield
end
def hide (&block)
actions[:hides]+=1
print"#{name} hides!"
yield
end
end
2 Answers
Angela Visnesky
20,927 PointsHi Melissa, In your initialize method, you set the @actions variable to be equal to a hash consisting of keys and values. The keys are scares, screams, hides, and runs, and the value of each is zero. Your program is keeping count of each of those items. When you call the hash key to increment the value or to print the hash_key, the syntax is variable[ :hash_key]. I hope this answers your question.
melissaorgill
Front End Web Development Techdegree Student 11,136 PointsMakes sense, Thank you Angela!