Bummer! You must be logged in to access this page.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Build a Grocery List Program: Part 1 3:15
- Create a Method That Returns a Hash 1 objective
- Build a Grocery List Program: Part 2 3:58
- Create a Method That Returns an Array 1 objective
- Build a Grocery List Program: Part 3 3:47
- Method Returns with Hashes and Arrays 1 objective
- Build a Grocery List Program: Part 4 4:17
- Working with Hashes That Contain Arrays 1 objective
- Ruby Collections: Review 6 questions

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Now that we know how to use arrays and hashes, we're going to build a small program that makes a grocery list for us. In this video, we'll modify our program to output our list for us.
Code Samples
At the end of the video, your code should look like this:
def create_list
print "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 much? "
quantity = gets.chomp.to_i
hash = { "name" => item_name, "quantity" => quantity }
return hash
end
def print_list(list)
puts "List: #{list['name']}"
puts "----"
list["items"].each do |item|
puts "Item: " + item['name']
puts "Quantity: " + item['quantity'].to_s
puts "---"
end
end
list = create_list()
puts list.inspect
list['items'].push(add_list_item())
puts list.inspect
print_list(list)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Joshua Goss
5,714 PointsWhy can the instructor print "list" with interpolation before we've defined it with a value below? It's just okay?
Posted by Joshua GossJoshua Goss
5,714 Points0 Answers
-
bfibbs
5,794 Points1 Answer
-
Nick Evershed
6,429 Points1 Answer
-
Shoko Ishigaki
21,826 Points1 Answer
-
nathanielcusano
9,808 Points3 Answers
-
darrell mayson
5,215 Points2 Answers
-
Kevin Liu
5,589 Points2 Answers
-
MICHAEL P
5,191 PointsI don't understand why in the code, it is written "----" ; and later "---". Can someone please clear this up?
Posted by MICHAEL PMICHAEL P
5,191 Points2 Answers
-
Michael Lazarz
15,135 Points2 Answers
-
Sean Flanagan
33,236 Points1 Answer
-
Quanton poulo
6,361 Points1 Answer
-
Asa Smith
10,009 PointsUndefined method 'push' I keep getting this error. Any idea why?
Posted by Asa SmithAsa Smith
10,009 Points3 Answers
-
JJME Dynamos
10,609 PointsBuild a Grocery List Ruby - Why do we need .to_s to display array
Posted by JJME DynamosJJME Dynamos
10,609 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up