Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Thomas Salai
Thomas Salai
4,389 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,657 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,389 Points

Hi Sean,

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