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

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Method returns with hashes and arrays.

I feel like I should be writing more code, but I'm not sure. Here is the instructions (I'm on step 1):

"Modify the "create_shopping_list" method to return a hash with the following keys and values: 'title': A string with the value "Grocery List" 'items': An empty array"

shopping_list.rb
def create_shopping_list
  puts "Title: " + "Grocery List"
  puts "Items: " + [].to_s
end

2 Answers

Jose Rosado
Jose Rosado
3,090 Points

Caleb,

Remember that create_shopping_list method should return a Hash.

def create_shopping_list
  # hashes are created with brackets {}. E.G: new_hash = {} 
  hash = { 
    "title" => "Grocery List", # this is a key / value pair. In this case, "title" is the key and "Grocery List" is the value
    "items" => []  # the key 'item' is an empty array.
  }
end
Jose Rosado
Jose Rosado
3,090 Points

Also, you can use numbers and symbols as the key.

new_hash = {
  1 => "This is a value",
  :name => "Jose" # :name is a symbol.
}

Read this to understand Hashes better.