ruby - Creating a Hash with a method -


i having trouble creating method establish new hash. know easier declare hash, need create method. here have far , keeps generating error message.

def create_new(hash_name) hash_name = hash.new   end 

this should create , empty hash^

def add_item(hash_name, item_name, item_quantity)  hash_name[:item_name.to_sym] = item_quantity end 

i keep getting error message on above code^ trying update hash , add new key value pair method

p create_new("grocery_list") 

this creates new empty hash^ when call below code says hash undefined

add_item(grocery_list, "pizza", "1")  p grocery_list 

in create_new method, define hash_name local variable. variable not exist anywhere body of method. that's seems confuse you.

you express better intent :

def create_new   hash.new end  def add_item(hash, key, value)    hash[key.to_sym] = value end 

in order trying do, have store result of method in kind of variable in order use :

grocery_list = create_new # grocery_list local variable  add_item(grocery_list, 'pizza', 1)