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

Thomas Salai
Thomas Salai
4,206 Points

I can't add my groceries Item to the array.

I can't add my groceries Item to the array. I got this error message :

Shoppinglist.rb:30:in `<top (required)>': undefined method `add_list_item' for main:Object (NoMethodError)
    from -e:1:in `load'
    from -e:1:in `<main>'
class Shoppinglist

  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

end

shopping = Shoppinglist.new

list = shopping.create_list()
puts "My List :" + list.inspect

#puts "My Shopping List " + shopping.add_list_item.inspect
list['items'].push(add_list_item())
puts "My hash : " + list.inspect

Many thanks in advance.

Regards, Thomas

2 Answers

Sean T. Unwin
MOD
Sean T. Unwin
Treehouse Moderator 28,639 Points

In your second last line,

# Change
list['items'].push(add_list_item())
# To
list['items'].push(shopping.add_list_item())
Thomas Salai
Thomas Salai
4,206 Points

Hi Sean,

Thanks a lots.! Yes, it make sense. It work now.