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

Grocery list not showing

I have been doing the Ruby track and am stuck on this part: Build a Grocery List Program: Part 3. I have been working along with the course, but when I ask for a grocery item, then quantity, it repeats itself and asks again for an item and a quantity and then just uses the second entry. Then, when I try and print the grocery list in the formatted version, as Jason does at the end of the video, nothing happens.

After pulling my hair out, I copied and pasted the code snippet from below the video, but it still fails to run correctly (asks for item and quantity twice and does not output the formatted list).

Am I missing something or do I have something missing? I am running ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14].

Any ideas?

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Can you post the code, or a link to a workspace snapshot?

I cannot seem to post an image, but here is the code:

shopping_list.rb

def create_list
  puts "What is the list name? "
  name = gets.chomp

  hash = { "name" => name, "items" => Array.new }
  return hash
end

def add_list_item
  print "What is the item called? "
  item_name = gets.chomp

  print "How many? "
  quantity = gets.chomp.to_i

  hash = { "name" => item_name, "quantity" => quantity }
  return hash
end

list = create_list()
puts list.inspect
puts add_list_item.inspect
list["items"].push(add_list_item)

TERMINAL

$ruby shopping_list.rb
What is the list name?                                                                                                              
Groceries                                                                                                                           
{"name"=>"Groceries", "items"=>[]}                                                                                                  
What is the item called? Apples                                                                                                     
How many? 12                                                                                                                        
{"name"=>"Apples", "quantity"=>12}                                                                                                  
What is the item called? Bananas                                                                                                    
How many? 3

You will see that "What is the item called?" and "How many?" is shown twice.

2 Answers

Seth Reece
Seth Reece
32,867 Points

Hi Chris,

It looks like you are printing out your list before you push your items into it. Move the line list["items"].push(add_list_item) above your puts statements. Also, since add_list_item is a method it needs parentheses after it. e.g.

list = create_list()
list["items"].push(add_list_item())
puts list.inspect

Thanks Seth :)