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

Need help

Totally confused here.

shopping_list.rb
def create_shopping_list
  Hash.new(create_shopping_list)   
  return hash
end

2 Answers

Stephen Gheysens
Stephen Gheysens
11,935 Points

Hi Marguerite,

Your function is trying to return a variable "hash" that doesn't exist.

Inside of the function, your first line Hash.new(create_shopping_list) is creating a hash, then trying to insert create_shopping_list into it. For an empty hash, just use Hash.new().

After you create the empty hash, you need to return it. you can do this in two lines (by assigning the new hash to a variable) or one line (return the new hash directly):

def create_shopping_list
  h = Hash.new()
  return h
end

or

def create_shopping_list
  return Hash.new()
end

Let me know if you have any questions!

Thanks, Stephen

Thanks so much for the clarification.