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 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,868 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!

I have losted at making grocery list :(