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 Blocks Blocks Practice Build a Monster Class: Part 3

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Quick question about the hashes in this class.

class Monster
  attr_reader :name, :actions

  def initialize(name)
    @name = name
    @actions = {
      screams: 0,
      scares: 0,
      runs: 0,
      hides: 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

The score board throws me off alittle. Like when its calling the hashes why is the : before screams and scares and the other hash keys rather then infront of them?

I've formatted your code as Markdown so it's readable - please checkout the 'Markdown Cheatsheet' link to learn how to do this (it's not your fault - in my opinion, Treehouse really haven't made this clear enough).

1 Answer

hash1 = { screams: 0, scares: 0 }
hash2 = { :screams => 0, :scares => 0 }
hash1 == hash2
=> true

In other words, hash1 and hash2 are exactly the same; they are just written using different syntax.

The syntax used in hash1 is newer Ruby syntax, presumably created for convenience - it's quicker to write.