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 Ruby Collections Build a Grocery List Program Build a Grocery List Program: Part 2

Thomas Salai
Thomas Salai
4,964 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
Sean T. Unwin
28,690 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,964 Points

Hi Sean,

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