I am having trouble creating a method to establish a new hash. I know that it is definitely easier just to declare the hash, however I need to create a method. Here is what I have so far and it keeps generating an error message.
hash_name = Hash.new
def add_item(hash_name, item_name, item_quantity)
hash_name[:item_name.to_sym] = item_quantity
add_item(grocery_list, "pizza", "1")
create_new method, you define a
hash_name local variable. This variable does not exist anywhere but the body of your method. That's what seems to confuse you.
You could express better your intent with :
def create_new Hash.new end def add_item(hash, key, value) hash[key.to_sym] = value end
In order to get to what you are trying to do, you will have to store the result of your method in some kind of variable in order to use it :
grocery_list = create_new('unused variable') # grocery_list is now a local variable add_item(grocery_list, 'pizza', 1)