Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.