Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Playing with the Grocery List - enhancing the hash

Hello together,

I am toying with the grocery list from the ruby collection session.

I am trying to write an inventory list. Date / Room / Items, QNT

I have written a 3rd. method to enter the date but I am getting an error undefined error. Et the end I like to have multiple dates, rooms with items within. I am not sure where the error is coming from. Can somebody help to fix my code?

def def_date #date to be entered in number. Date format will come later print "Enter date: " date = gets.chomp.to_i hash = {"date" => date, "room" => Array.new} end

def def_room print "Enter room name: " room_name = gets.chomp hash = {"room" => room_name, "item" => Array.new} end

def def_item print "Enter Item: " item_name = gets.chomp

print "Enter Quantity:"
quantity = gets.chomp.to_i

hash = {"item" => item_name, "quantity" => quantity} end

inventory = def_date() inventory['room'].push(def_room()) puts inventory.inspect inventory['item'].push(def_item()) puts inventory.inspect

Thanks a lot in advance

Jens

instead of: -> inventory['item'].push(def_item()) I have entered: -> inventory[:item] = def_item()

and now the program runs at least w/o errors.

What do I need to add when I like to add multiple items?

I have tried to run " inventory[:item] = def_item() " twice but it just overwrites the previous entry

5 Answers

Ilya Dolgirev
Ilya Dolgirev
35,375 Points

Could you repost your code using Markdown? Now it's really hard to read it :)

Sorry, you're absolutely right - what a difference :-)

def def_date  #date to be entered in number. Date format will come later
  print "Enter date: "
  date = gets.chomp.to_i
  hash = {"date" => date, "room" => Array.new}
end

def def_room
  print "Enter room name: "
  room_name = gets.chomp
  hash = {"room" => room_name, "item" => Array.new}
end

def def_item
  print "Enter Item: "
  item_name = gets.chomp

  print "Enter Quantity:"  
  quantity = gets.chomp.to_i

  hash = {"item" => item_name, "quantity" => quantity}
end

inventory = def_date()
inventory['room'].push(def_room())
inventory[:item] = def_item()
inventory[:item] = def_item()
puts inventory.inspect
Ilya Dolgirev
Ilya Dolgirev
35,375 Points

OK, that's much better :) What the problem you have with that?

I like to add multiple items to the list but when calling the method multiple times it just overwrites the one before.

Ilya Dolgirev
Ilya Dolgirev
35,375 Points

Well, it little messy anyway :) First of all, it's redundant to use brackets when call a methods. Ruby is smart enough to understand what you're trying to do :)

One little hint for you:

inventory["room"][0]["item"].????? # Here we access the first room in array of rooms by index, then getting a value of a `item` key of a hash(another Array).

And according to your code above, you've already known how to add items to the arrays :)

Ilya - Thanks a lot - will try it.