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 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
PLUS
Rick Buffington
Courses Plus Student 8,037 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
PLUS
Rick Buffington
Courses Plus Student 8,037 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