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 trialAlphonse Cuccurullo
2,513 PointsUsing the values_at method, create an array called grocery_list with the value of the grocery_item hash at the "item" ke
What did i do wrong here?
4 Answers
Andrew Patterson
3,075 PointsNo problem man! Happy to help.
Although Ruby doesn't really pay attention to whitespace, it is good to separate our code into separate lines to keep track of the sequence of code and the logic behind what we are doing.
I'm going to separate out and number these lines:
- grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company","food" => true}
- grocery_item.has_value?("food")
- grocery_item.values_at("item", "quantity")
- grocery_list = []
- grocery_list.store("item")
The first part of the challenge asks "Using the has_value? method, check to see if the grocery_item hash has a value called "Bread". If it does, set a new key in the hash called "food" with the value of true."
So you will need lines 1 and 2 but make sure you are checking for "Bread" instead of "food." Let's also make it a conditional statement:
if grocery_item.has_value?("Bread") grocery_item["food"] = true end
The second part of the challenge asks "Using the values_at method, create an array called grocery_list with the value of the grocery_item hash at the "item" key."
Let's combine lines 3, 4, and 5 into one line to get what we want:
grocery_list = [grocery_item.values_at("item")]
basically, I'm making a new array, and pushing in the value at "item" in one step here.
Let me know if you have any other questions about this code. There's always another way to do it, so you may find another way.
One thing I would warn against is using .store on grocery_list since .store is meant to be used with a Hash, but for us grocery_list is an array.
Happy Coding!
Andrew
Andrew Patterson
3,075 PointsHey! Sorry I've been out for a while. So it sounds like you are passing the second challenge but not the first? Instead of hard coding the "food" => true line, I made an if statement:
if grocery_item.has_value?("Bread") grocery_item["food"] = true end
which goes on the line before the creation of grocery_list = {}.
Check your closing bracket "]" on the last line and you can delete the grocery_list.store("item") part. Please mark this as the top answer if it works for you!
Andrew Patterson
3,075 PointsHello Alphonse,
I am not seeing your code, but I would like to offer my help. Here's a resource you may find helpful for this question:
http://ruby-doc.org/core-1.9.3/Hash.html#method-i-values_at
The Ruby docs say that the values_at method returns an array, so you may want to call values_at("item") for each grocery_item in order to make arrays with the grocery items within them. Combine the arrays to get a single grocery_list.
Let me know if this helps!
Happy Coding,
Andrew
Alphonse Cuccurullo
2,513 PointsHey man that site did help but im still a tad confused.......
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company", "food" => true} grocery_item.has_value?("bread") grocery_list = {""} grocery_list.values_at("grocery_item" => "item")
This is what i did.
Andrew Patterson
3,075 PointsIt might help to pseudocode what we want to happen here before we get to the code syntax. Here's what I've gathered about the problem:
Given a grocery_item, you want to be able to add the value from the key "item" to a new array called grocery_list.
Input: grocery_item (a hash) Desired Output: grocery_list (an array of strings)
To get from the item to the list, we need to
- Create an empty grocery list array
- Store the value of the key "item" (in a variable perhaps?)
- Put that stored value in the list
A few things I noticed about the code in your response:
grocery_item.has_value?("bread") will return false because Bread is capitalized in the hash. Can you tell me a little more about what this line is doing for the code?
grocery_list = {""} seems to be you are making an empty hash, but I think you may want an empty array like this: grocery_list = []
Lastly, make sure grocery_list.values_at("grocery_item" => "item") should only take the key, so it would be more like grocery_list.values_at("item").
If this is helpful, please consider an upvote! It will help me get points in the career track. Thank you and good luck!
Alphonse Cuccurullo
2,513 PointsHey man just like to say i appreciate the help. Im still ff with something here but i think im progressing.
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company","food" => true} grocery_item.has_value?("food") grocery_item.values_at("item", "quantity") grocery_list = [] grocery_list.store("item")
I think im off with the at method but having issues knowing where to execute it. Kind regards
Alphonse Cuccurullo
2,513 PointsAlphonse Cuccurullo
2,513 Pointsi really appreciate it man im actually starting to get it a bit......
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company", "food" => true } grocery_item.has_value?("Bread") grocery_list = {} grocery_list.store ("item") grocery_list = [grocery_item.values_at("item")
so when i enter this it tells me task 1 isnt passing how can i make it pass?
Alphonse Cuccurullo
2,513 PointsAlphonse Cuccurullo
2,513 Pointsi really appreciate it man im actually starting to get it a bit......
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company", "food" => true } grocery_item.has_value?("Bread") grocery_list = {} grocery_list.store ("item") grocery_list = [grocery_item.values_at("item")
so when i enter this it tells me task 1 isnt passing how can i make it pass?
Alphonse Cuccurullo
2,513 PointsAlphonse Cuccurullo
2,513 Pointsi really appreciate it man im actually starting to get it a bit......
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company", "food" => true } grocery_item.has_value?("Bread") grocery_list = {} grocery_list.store ("item") grocery_list = [grocery_item.values_at("item")
so when i enter this it tells me task 1 isnt passing how can i make it pass?