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 Create a Method That Returns a Hash

seriously confused... asking to create simple method "create_shopping_list" and only return a hash... not working...

in the build a grocery list program, stage 3 challenge... it asks for a simple method called "create_shopping_list" with no standard inputs, just return the hash...

def create_shopping_list

hash = { "name" => name }

return hash end

what's wrong with this code?

shopping_list.rb
def create_shopping_list
  hash = { "name" => name, "items" => Array.new }
  return hash
end

4 Answers

ARNOLD SANDERS
ARNOLD SANDERS
5,266 Points

I would write it like this:

def create_shopping_list 
     hash = Hash.new
    return hash
end

and then call it:

create_shopping_list
William Li
PLUS
William Li
Courses Plus Student 26,858 Points

This is actually pretty simple, it just ask you to return a hash, any hash would do, including an empty hash. :smiley:

def create_shopping_list
  {}
end

And the problem of your code is this line "name" => name, you'll get a NameError exception because name is undefined.

got it thanks!

Ivan Kusakovic
Ivan Kusakovic
12,197 Points

I have losted at making grocery list :(