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 make use of the methods we've written.
Code Samples
Here's what our code looks like now:
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
list = create_list()
puts list.inspect
list['items'].push(add_list_item())
puts list.inspect
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Shreemangal Sethi
Full Stack JavaScript Techdegree Student 15,552 PointsHow does the 2nd # add_list_item() gets called without explicitly calling it?
1 Answer
-
A M
1,397 Points1 Answer
-
Nick Evershed
6,429 Points3 Answers
-
MICHAEL P
5,191 PointsKeep Getting "Undefined method 'push' for nil: NilClass error" - Not Sure Why?
Posted by MICHAEL PMICHAEL P
5,191 Points3 Answers
-
Robert Smith
2,144 Pointsget.chomp.to_i always outputs the number 0, regardless of the string input. My code is below with the output following.
Posted by Robert SmithRobert Smith
2,144 Points1 Answer
-
Oğulcan Girginc
24,848 Points3 Answers
-
Paulo Moreira
2,963 Points1 Answer
-
Alphonse Cuccurullo
2,513 Points1 Answer
-
Thomas Salai
4,964 Points2 Answers
-
Jimmy Mannan
5,201 Points4 Answers
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