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

Tyler Wilson
Tyler Wilson
1,675 Points

I can't quite understand what is being asked. Every time I add a line to this code it claims that task 1 no longer runs.

Need a starting point.

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

if true then grocery_item.store("food", true)


else
end

3 Answers

Tim Knight
Tim Knight
28,888 Points

Tyler,

You just have to combine your conditional check with the creation of the food item in the hash, give this a shot.

if grocery_item.has_value?("Bread")
  grocery_item.store("food", true)
end

You can also write that using:

if grocery_item.has_value?("Bread")
  grocery_item["food"] = true
end
Tyler Wilson
Tyler Wilson
1,675 Points

Ruby is even simpler than they let on, thanks.

What I was more confused with was where to start for the value_at method that is asked in the next question. Sorry for not clarifying. Where am I putting the value_at method and what is being asked that I do?

Original question: Using the values_at method, create an array called grocery_list with the value of the grocery_item hash at the "item" key.

Tim Knight
Tim Knight
28,888 Points

No problem Tyler, you'd just add that at the bottom after the conditional, so you're full code would look something like this:

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

if grocery_item.has_value?("Bread")
  grocery_item["food"] = true
end

grocery_list = grocery_item.values_at("item")
Tyler Wilson
Tyler Wilson
1,675 Points

Gotcha, just the wording of the question that threw me off. Thank you for your help.

So, I have exactly this code and it still won't pass. Suggestions??

Tyler Wilson
Tyler Wilson
1,675 Points

Could you post the code you're running just to make sure? I copied and pasted the last response from Tim Knight and it ran fine.

@tyler wilson I actually got it to work - but thank you