2016-04-22 81 views
-1

所以我想創建一個菜單類,其中包括顯示價格。從方法調用哈希值

我想創建一個方法(show_price)顯示的價格

class Menu 

    def initialize menu = { pepperoni: 50, vegetariana: 50, mexicana: 50, deluxe: 100 } 
    @list = menu 
    end 


    def show_price item 
    item = ":" + "#{item}" 
    list[item] 
    end 


    attr_reader :list 

end 

可惜這只是給了我零,沒有任何人知道這樣做的正確方法?

回答

3
def show_price item 
    item = item.to_sym 
    @list[item] 
end 
0
class Menu 
    attr_reader :list 

    def initialize menu = { "pepperoni"=> 50, "vegetariana"=> 50, "mexicana"=> 50, "deluxe"=> 100 } 
    @list = menu 
    end 


    def show_price item 
    @list[item] 
    end 

end 

menu = Menu.new() 
puts "Deluxe costs: #{menu.show_price("deluxe")}"