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 Method Returns with Hashes and Arrays

Brian Patterson
Brian Patterson
19,588 Points

So why is this giving me an error?

def create_shopping_list hash = { 'title' => Grocery List, 'items' => Array.new } return hash end

shopping_list.rb
def create_shopping_list
  hash = { 'title' => Grocery List, 'items' => Array.new }
  return hash
end

2 Answers

Rick Buffington
Rick Buffington
8,146 Points

You are almost there. Your syntax for the hash is correct, however you are missing some quotes/apostrophes around the actual title of the list. Ruby is stuck on the word Grocery and then domino's after that.

hash = { 'title' => 'Grocery List', 'items' => Array.new
Brian Patterson
Brian Patterson
19,588 Points

So why in the Shopping list example he does not put name in quotes ? ' def create_list print "What is the list name ? " name = gets.chomp hash = {"name" => name, "items" => Array.new } return hash end '

Rick Buffington
Rick Buffington
8,146 Points

The difference is that he is assigning what you type in the console to a variable named "name" - he then uses that variable in the hash. You are are trying to assign a string. Example:

name = "John"   # We are assigning a string to name (you could use gets.chomp like the video)
hash = { "name" => name, "items" => Array.new }    # Here, we use the key "name" and assign it the variable name
# Instead of using a string assignment for value, he is using a variable

The above is the same thing as this without using a variable (like what you were trying to do):

hash = { "name" => "John", "items" => Array.new }
Brian Patterson
Brian Patterson
19,588 Points

Ah I see. The variable name is name = gets.chomp