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

What is wrong with my case statement?

Calling this method results in either no console output at all or 3 lines of output that looks exactly like this:

Trenchcoat!11
Backpack!
Gun!

I don't understand why execution continues to evaluate all of the when statements when only one possible when statement applies at any given time. Here is my code:

  def on_blackmarket(params)
    case params[:item]
    when :storage_small
      puts "Trenchcoat!11"
    when :storage_large
      puts "Backpack!"
    when :gun
      puts "Gun!"
    else
      puts "Nothing!"
    end
  end

1 Answer

oh shoot, I figured out the method is being called 3 times when it does get called on random chance and each time it has a new value.