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

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.