Ruby Ruby Collections Build a Grocery List Program Build a Grocery List Program: Part 2

Jimmy Mannan
Jimmy Mannan
5,201 Points

Why we need this first hash?

In this code from https://teamtreehouse.com/library/ruby-collections/build-a-grocery-list-program/build-a-grocery-list-program-part-2

why we need this first hash?

def create_list
  print "What is the list name? "
  name = gets.chomp

  hash = { "name" => name, "items" => Array.new }
  return hash
end

and in the second part create another hash with the same name and same key of "name"?

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
Jimmy Mannan
Jimmy Mannan
5,201 Points

I am not sure why the backticks are not adding a blackbox here

William Li
William Li
Pro Student 26,814 Points

fixed the code highlighting for you. For further reference, it's necessary to leave a blank line before and after the code block in order for it to display properly in Markdown.

4 Answers

William Li
PRO
William Li
Pro Student 26,814 Points

Hi, Jimmy Mannan

and in the second part create another hash with the same name and same key of "name"?

This hash and that hash aren't the same object. Notice that the hash variable was created within the function body? That makes it local variable, it belongs solely to the function in which it was defined, nothing outside the scope of the function have access to it.

These 2 hash variables in the code, asides from sharing the same variable name, have no connection with each other; they are 2 completely different object. Think of it this way, many people in this world share the same name, but they aren't the same person.

Hope this helps.

Jimmy Mannan
Jimmy Mannan
5,201 Points

Thank you William for the tip on backtick usage.

I see it now..about one being local variable and the other global.

I am still not sure why we needed to create the first hash..what has it done for the programme. I think I am missing something very basic about Ruby hash and arrays and methods as I am having to constantly look up outside treehouse to understand what is going on in the tutorials.

Thanks for your help

William Li
William Li
Pro Student 26,814 Points

about one being local variable and the other global.

actually from the 2 code snippets you posted above, both are local variables, one was defined within the function body of create_list; another at the body of add_list_item; therefore, they are both local variables.

I am still not sure why we needed to create the first hash..what has it done for the programme.

You're indeed correct, there's no need to create that hash local variable, you can write it as such.

def create_list
  print "What is the list name? "
  name = gets.chomp

  return { "name" => name, "items" => Array.new }
end

Now it return the Hash directly, no need for an extra local variable; same can be done for the 2nd code snippets as well.

Now, because this is the basic Ruby course, Jason is trying to write code in a very verbose way so that the students can have better understanding of the concepts.

Jimmy Mannan
Jimmy Mannan
5,201 Points

hmmmmm.......okay!

:-) thanks William

Jimmy Mannan
Jimmy Mannan
5,201 Points

I have tried to figure it out on my own but nopey...cant do it...5 days I am stuck on this grocery list...I have no clue whats going on here... from the first method itself I am lost! do you think you will have time to break down this programme and explain to a dummy...

William Li
William Li
Pro Student 26,814 Points

Sorry, Jimmy, quite busy these days, let me tag other Ruby experts on the forum, hopefully they will have time to break down the problem and explain it in more detailed to you :)

cc Amanda Wagner , Salman Akram , Tim Knight

Hi Jimmy, what exactly are you having trouble with?

Jimmy Mannan
Jimmy Mannan
5,201 Points

Thanks Amanda.

Well when I look at the code, I cant understand the purpose or reason for anything written there. My enrolment is paused so I cant watch the video again for anymore insights. (though my enrolment is paused or was paused by me as I was stuck on this list).

I figured if I try to make this list from scratch maybe I will understand the reasons behind the various methods and the scary hashes and arrays. Following I came up with that seems to serve the purpose of a rudimentary list but it is nothing like the original code:

print "What is the list name? "
list_name = gets.chomp

list = Hash.new

loop do
puts "What is the item called? "
item = gets.chomp
break if item == ""
puts "how much?"
quan = gets.chomp.to_i
list[item] = quan
end

puts"\t"
puts list_name
puts "-" * 80

list.keys.each do |key|
 puts "\tItem: " + key + "\t\t\t" + 
      "Quantity:" + list[key].to_s
 end
puts "-" * 80 

but I would like to understand the original code about why the hash was created in the first method and what was its purpose in the second method. why the methods that were created were created.

I understand I am asking for too much time here and its okay if you dont have that... I guess then I will just go back to the video and watch it a few dozen times more :-)

John Fisher
John Fisher
7,974 Points

I know this is quite an old thread now, and I'm no expert with Ruby (Just up to this lesson myself), but here is how I see this. Someone correct me if I'm wrong.

The hash in the first method is storing two things; the actual name of your list in the first part (ie "My Grocery list", or "Things I need to buy" or whatever), then, in the "items" part of the first hash, that is where you will "push" all you items and quantities which you are asked to provide in the second method (the add_list_item function).

I thought of it like this...

Imagine the first function and its hash as an actual piece of paper. You take a pen and write "Shopping List" ("name") at the very top of that piece of paper. The blank part on this piece of paper, underneath where you wrote "Shopping List", is the space where you will later write your list ("items"). This title and the space for the list are your first hash.

Imagine the second method and its hash as all the things you need to buy, but they are only in your head, so you need to write them (or .push them) on to that space you left for them on your piece of paper (or, the first method's hash).

I'm not 100% sure, but I think that's how the program is working, and why two separate hashes have been used in this example.

Does that make any sense?

The explanation is way too fast with no logical understanding. Could you please break down this lesson so any beginners understand what you are doing ? I guess that this is the idea behind this course ! To teach the beginers ..