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

Brian Patterson
Brian Patterson
19,588 Points

Why isn't this working?

'def create_shopping_list hash = { "name"=> name } return hash

end ' Not sure why this does not work?

def create_shopping_list
  hash = { "name"=> name }
  return hash


1 Answer

Rick Buffington
Rick Buffington
8,146 Points

From what I can tell, it looks like you are trying to assign the key "name" with a value that Ruby thinks is a variable. Ruby is looking for a variable named "name" because you have not enclosed the value in quotes - or have not assigned a value to name.

hash = { "name" => "name" }

If what you are trying to do is assign the value with the "name" variable, you'd need to declare and assign it before using it in the hash:

name = "John"
hash = { "name" => name }

Hope that helps.

Brian Patterson
Brian Patterson
19,588 Points

Thanks now stuck on the next bit.