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 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
So when we just left off we had
created a shopping list and
0:00
printed out the contents.
0:03
Now let's go ahead and add on to this and
add an item to this list.
0:05
So let's go ahead and
call add list item dot inspect, and
0:11
what that's gonna do is the call
the add list item method and
0:15
then we're just gonna see
what the output is first.
0:20
So we click down into the console and
type rubyshoppinglist.rb.
0:26
The list name is groceries, and we don't
have any items, so let's go ahead and
0:30
add an item.
0:34
And the item is milk.
0:35
Uh-oh.
0:38
Undefined local variable or
method name for main object.
0:39
That's because we spelled it wrong.
0:44
Our program freaked out a little bit,
and that's okay.
0:47
It exited, but
nothing catastrophic happened.
0:51
So let's go ahead and
clear the screen, and run that again.
0:53
And actually, let's put a space here,
format that just a little bit better.
0:56
List name is Groceries, and go ahead and
add milk to our grocery list.
1:01
And we can see that we return
a new hash with the key of name,
1:06
and the value of milk.
1:12
And that's okay, but what I think I would
like to do is also get the quantity back.
1:15
So let's go ahead and ask how much.
1:22
And we'll get from standard input again,
and
1:28
then call to I on that,
which will turn it into a number.
1:32
And now we'll put that into the hash.
1:40
Now let's go ahead and
run this again and see what happens.
1:44
List name is Groceries.
1:48
Item is milk.
1:51
And I only want one.
1:53
Now we can see that this add list item
method is returning a hash with two keys,
1:56
name and quantity.
2:01
Now let's go ahead and take a look
at our list and see how it looks.
2:04
Gonna clear my screen here and
run the program again.
2:10
So here is the item that we're returning,
but
2:19
it's not being added to our list
of items inside of the hash.
2:22
So let's go ahead and fix that.
2:29
What we're gonna do is use the push method
and the reason is items is an array.
2:32
So let's go ahead and type list.push, and
2:40
then the argument that we're
adding to push is add list item.
2:44
Now, we can print out
the contents of the list again.
2:54
Let me clear my screen here and
run it again.
3:01
Okay, we've got our grocery list created,
milk, and we only need one.
3:05
Uh-oh, undefined method push for
this hash.
3:12
Now what could that be?
3:17
Well, we're not pushing
onto the list hash.
3:20
We're pushing onto the list
items inside of the hash.
3:24
Now let's clear this and run it again.
3:31
Okay.
3:38
Now this is what our
grocery list looks like.
3:40
It's a hash with two keys,
name which is Groceries,
3:43
items, which is an array,
which contains a hash.
3:49
In our next video we're
going to format this.
3:55
You need to sign up for Treehouse in order to download course files.
Sign up