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

Help please!

I have been having a lot of trouble with working with arrays and if anyone could clarify what is wrong with this code that would be great

thx isaacpollock

hash.rb
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
if grocery_item.has_value?("Bread") 
  grocery_item ["food"] = true
  values_at.array.new = grocery_list = grocery_item grocery_item.has_value? ("item")
end

1 Answer

Adam Sommer
Adam Sommer
62,470 Points

First thing I notice is this line:

  values_at.array.new = grocery_list = grocery_item grocery_item.has_value? ("item")

Having two assignment operators ("=") is usually a bad things. The next thing is that the values_at is a method of the Hash class. So you'd need to call it on a hash like the grocery_item hash:

grocery_item.values_at

The values_at method also takes an argument of key so you can't call array.new at the end of it. If you're trying to return the value of the grocery_item hash with key item you can do something like:

grocery_list = grocery_item.values_at('item')

You'll have a new array in grocery_list.

Hope that helps clarify some things.