text: Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
  Build a Grocery List Program: Part 13:15 with Jason Seifer
  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 add methods to create our list and also add items to it.
  Code Samples
  Here is the code we write in the video. Follow along using Workspaces!
  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 hash = { "name" => name } return hash end list = create_list() puts list.inspect
  - 0:00
  [SOUND] Now that we know how to use arrays and hashes,
  - 0:05
  let's write a simple program that uses everything we've used so far.
  - 0:12
  We're going to do this over a few different videos, so
  - 0:15
  be sure to follow along using workspaces.
  - 0:18
  Okay, now that we know how to work with arrays and hashes, let's go ahead and
  - 0:23
  write a simple shopping list program.
  - 0:26
  So I've gone ahead and launched a new Ruby workspace.
  - 0:30
  Now, let's go ahead and create a new file.
  - 0:33
  And we'll call this shopping list.rb.
  - 0:38
  Now let's go ahead and create a shopping list.
  - 0:42
  We'll create a method that asks for the name of the list, and then,
  - 0:46
  we can return a hash of the name and the items inside of the shopping list.
  - 0:54
  So we'll call that method, create list.
  - 1:00
  Now let's go ahead and ask what the list's name is.
  - 1:08
  And we will take from standard input, the name of the shopping list and
  - 1:13
  remove any external characters from it that are white space at the end.
  - 1:19
  Now let's go ahead and return a new hash.
  - 1:26
  We will give the hash a key of name and a key of items.
  - 1:34
  Which is a new array.
  - 1:36
  And then, we'll go ahead and return this hash.
  - 1:42
  Now that we have a method to create the list, let's go ahead and
  - 1:46
  create a method to add an item to the list.
  - 2:01
  So now we're going to add a list item, and
  - 2:03
  we'll get the item name from standard input.
  - 2:08
  And then we'll create a new hash, and we can return that hash.
  - 2:15
  Go ahead and save that.
  - 2:17
  And now, let's go ahead and ask the user.
  - 2:21
  Or their list.
  - 2:23
  So we can say, list and then an equals sign, and
  - 2:28
  we'll call the method create list.
  - 2:34
  Now what that will do is ask the user for the name of the shopping list,
  - 2:39
  and return a hash with the key of name and the key of items, which is an array.
  - 2:48
  And let's go ahead and print out list.inspect.
  - 2:53
  Now let's click down in the console and run this shopping list program.
  - 2:58
  The list name is groceries.
  - 3:01
  Now we can see we've got this list hash,
  - 3:05
  with the name of what we input in an empty array for items.
  - 3:11
  In the next video, we'll start adding items to the list.