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 Hash Methods

Mateusz Hyla
Mateusz Hyla
4,658 Points

I don't understand whats wrong with merge method

HI I got error :

Bummer! The merge method was not called on the grocery_item hash.

How can this be when in code you see I call merge on grocery_item hash ?

This is documentation example :

h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 254, "c" => 300 } h1.merge(h2) #=> {"a"=>100, "b"=>254, "c"=>300}

I tried to do the same like :

grocery_item.merge(calories) final_item.merge(grocery_list)

But I noticed that they dont merge at all :-p. You can see it when you inspect this hashes after merge.

Is that some kind of Bug or something else or I made somewhere mistake ?

Cheers

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

grocery_item = grocery_item.merge(calories)
final_item = {}
final_item = final_item.merge(grocery_item)

2 Answers

I think it's a problem with the way they check the test. You're reassigning the grocery_item variable, making it harder to track for any validator. The code will work, but it's harder for them to tell.

A simpler way, without reassigning any variables, is just to assign the result of the initial merge to the final_item variable. As in:

final_item = grocery_item.merge(calories)

That should give you a solution that will pass the tests.

Mateusz Hyla
Mateusz Hyla
4,658 Points

Yeah its worked but its quite strange for me that some challanges on Treehouse has so narrow scope of possible proper answers and solutions. I hope they will build that up in future. I really like to learn with Treehouse thou :-) Thanks James ^_^

Mateusz Hyla
Mateusz Hyla
4,658 Points

Even overriden doesnt work like :

final_item.merge("item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" ,"calories" => 100)

Whats wrong with merge method ?