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 Collections Ruby Hashes Working with Hash Values

Chelsey Darrington
Chelsey Darrington
7,956 Points

Ruby Collections hash challenge: "The 'has_value?' method was not called"?

The task is to use the has_value? method to determine if the value "Bread" exists. If it does, set a new key in the hash for "food" and its value as "true". The error that is returned is "The 'has_value?' method was not called" but when I try it in irb it works. I'm pretty sure I'm calling the has_value? method, but obviously something is wrong. I even added it in specifically above the if-statement to make sure it would see it, but it still doesn't recognize it. Thanks in advance for the help!

Here is my code (first hash provided by Treehouse):

grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }

grocery_item.has_value?("Bread")

if grocery_item.has_value?("Bread") == true grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company", "food" => true } end

hash.rb
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }

grocery_item.has_value?("Bread")

if grocery_item.has_value?("Bread") == true
  grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company", "food" => true }
end

1 Answer

There's some stuff you can eliminate in here that the challenge probably expected you to eliminate.

hash.rb
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }

if grocery_item.has_value?("Bread")
  grocery_item["food"] = true
end
  1. I eliminated the second line of code. It doesn't change the effect at all and it is probably a good idea to remove.
  2. You don't need the == true part because the has_value?() method already returns a boolean (true or false)
  3. Lastly, and most importantly, you don't need so much code in the if block! The challenge probably doesn't want you to copy-and-paste and then add the value into that new hash. To add a value to a hash in Ruby, you just have to select the item from the hash (which doesn't exist) then assign a value to that.

I hope this helps. ~Alex

Chelsey Darrington
Chelsey Darrington
7,956 Points

Thank you so much for your help, Alex.

I knew I had way too much code there, and I definitely knew ruby would have a beautiful and much more succinct solution.

~Chelsey

You are welcome. :)

I like Ruby because of it's flexibility as well :smile: