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?

Andrew Stelmach
Andrew Stelmach
12,374 Points

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

Andrew Stelmach
Andrew Stelmach
12,374 Points
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.