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

Sarah A. Morrigan
Sarah A. Morrigan
14,329 Points

i thought of doing something to make it more interesting

#!/usr/bin/ruby

class Monster
    attr_reader :name, :actions

    def initialize(name)
        @name = name
        @actions = {
            screams: 0, 
            scares: 0, 
            run: 0, 
            hide: 0
        }
    end

    def say(&block)
        print "#{name} says "
        yield
    end

    def scream(&block)
        actions[:screams] += 1
        print "#{name} screams "
        yield
    end

    def scares(&block)
        actions[:scares] += 1
        print "#{name} scares "
        yield
    end
    def run(&block)
        actions[:run] += 1
        print "#{name} runs "
        yield
    end
    def hide(&block)
        actions[:hide] += 1
        print "#{name} hides "
        yield
    end
    def print_scoreboard
        puts "-" * 25
        puts "#{name} scoreboard"
        puts "-" * 25
        puts "- Screams: #{actions[:screams]}" 
        puts "-" * 25
        puts "- Scares: #{actions[:scares]}"
        puts "-" * 25
        puts "- Runs: #{actions[:run]}"
        puts "-" * 25
        puts "- Hides: #{actions[:hide]}" 
    end
end

monster = Monster.new("Fluffy")
xsay = rand(1..9)
xsay.times do
        monster.say do 
            puts "welcome to my home" 
        end
end
xscream = rand(1..100)
xscream.times do
        monster.scream do
            puts "BOO!"
        end
end
xscares = rand(1..100)
xscares.times do
        monster.scares do
            puts "Go away!"
        end
end
xrun = rand(1..100)
xrun.times do
        monster.run do
            puts "Going to get you!"
        end
end
xhide = rand(1..100)
xhide.times do
        monster.hide do
            puts "running away and hiding!"
        end
end
puts "\n"
puts monster.print_scoreboard

:)

1 Answer

Brandon Keene
Brandon Keene
7,217 Points

Fluffy is one cowardly monster:

-------------------------
Fluffy scoreboard
-------------------------
- Screams: 4
-------------------------
- Scares: 15
-------------------------
- Runs: 12
-------------------------
- Hides: 99
Rachelle Wood
Rachelle Wood
15,362 Points

He is definitely the world's most terrifying monster.