Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
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 clean up and put the finishing touches on our grocery list program.
Code Samples
Here is our finished program:
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_separator(character="-")
puts character * 80
end
def print_list(list)
puts "List: #{list['name']}"
print_separator()
list["items"].each do |item|
puts "\tItem: " + item['name'] + "\t\t\t" +
"Quantity: " + item['quantity'].to_s
end
print_separator()
end
list = create_list()
puts "Great! Add some items to your list."
list['items'].push(add_list_item())
list['items'].push(add_list_item())
list['items'].push(add_list_item())
puts "Here's your list:\n"
print_list(list)
So when we last left off,
we had printed out our list.
0:00
And let's go ahead and run this again.
0:05
The item is milk and
we only want one thing of milk.
0:08
Okay, let's clean up how
this looks a little bit.
0:11
First, since we're sort
of duplicating code here,
0:15
by printing out a separator, let's go
ahead and make a method out of that.
0:19
And we'll call that
method output_separator.
0:24
You know what?
Let's call it print_separator.
0:31
And we'll give it an argument of
the character that we wanna print.
0:34
And we can go ahead and
give this a default value of a dash.
0:39
And all this method is going to do
is print that character 80 times.
0:46
Now let's go ahead and
0:55
replace the printing of
the separator with the method name.
0:56
Okay.
1:06
Clear my screen here and run this again.
1:09
You know what I can
take out the list here.
1:13
I can take out the list inspection
since we're printing it on our own now.
1:16
This name is Groceries, and
the item that we're adding is milk.
1:21
And 1.
1:26
Okay, now we have a separator.
1:27
But let's go ahead and
clean this up even a little bit more.
1:31
So now let's go ahead and
indent each line here.
1:35
So we'll print a tab character
Inside of this list and
1:38
the item's name and then another plus.
1:43
And let's say we'll do three tab
characters here and the quantity.
1:46
Now I want quantity to be
all on the same line so
1:51
I'm going to add another plus and
take out that put statement.
1:55
And this will create one big string and
2:03
send it in as an argument
to the puts method.
2:06
Let me clear my screen here, click in
to the console and run this again.
2:09
Okay, this is looking a little bit better.
2:20
We've got our grocery list, and
our item of milk, and our quantity of one.
2:22
Let's go ahead and
say that we want to add another list item.
2:28
Print my screen and run this again.
2:34
List name is groceries.
2:35
The item is milk, and we only want one.
2:38
Bread, we're gonna say we
want two loaves of bread.
2:40
And that looks good.
2:43
We've got a separator in
between each of the items.
2:44
But you know what?
2:48
Let's go ahead and print the separator at
the end of all the list items instead.
2:49
I think that'll look
just a little bit better.
2:53
And now let's go ahead and
just add another item to the list.
3:00
And in fact before we print the list,
I'm just going to print here's your list.
3:05
And a new line.
3:13
In fact let's go ahead and
add another message and say great.
3:16
Add some items to your list.
3:20
Now let me clear my screen here,
there's gonna be more output so
3:25
I'm gonna make this a little bit
bigger and type ruby.shoppinglist.rb.
3:28
This name is groceries Great.
3:35
Let's add some items to the list.
3:37
Milk, we only need one.
3:40
Bread, two loaves of bread.
3:42
Eggs, let's get three dozen eggs
because we're really hungry.
3:45
All right, and here is our grocery list
with our three different items and
3:50
the corresponding quantities.
3:55
In this course, we've learned about
how arrays and hashes work, and
3:58
have even learned some simple iteration.
4:02
Don't stop now, though.
4:05
Make sure to practice these
concepts on your own, and
4:07
practice by building your own programs.
4:10
If you need inspiration,
check out the extra credit.
4:12
Happy coding.
4:16
You need to sign up for Treehouse in order to download course files.
Sign up