Bir Sınıf Mağazam var, her Mağazanın farklı Ürünlere sahip bir Hash'i var ve her Ürünün farklı türde satışları olan bir Hash var. Bu verilerle Yakut sort_by Nesneler/Sınıflar undefined yöntemi 'sort_by' için
class Store
def initialize
@store_products = Hash.new
#@store_products['product_id'] = Product
end
end
class Product < Store
def initialize(model)
@model = model
@sell_option = Hash.new
#@sell_option['sell_name'] = SellOption
end
end
class SellOption < Product
def initialize(size, price, stock)
@size = size
@price = price
@stock = stock
end
end
:
product1 = Product.new("shirt1")
product1.add_sell_option(SellOption.new("S", 12, 10))
product1.add_sell_option(SellOption.new("M", 15, 10))
product1.add_sell_option(SellOption.new("L", 14, 10))
product2 = Product.new("shirt2")
product2.add_sell_option(SellOption.new("M", 6, 10))
product2.add_sell_option(SellOption.new("L", 7, 10))
product3 = Product.new("shirt2")
product3.add_sell_option(SellOption.new("M", 10, 10))
product3.add_sell_option(SellOption.new("L", 11, 10))
store = Store.new
store.add(product1)
store.add(product2)
store.add(product3)
sınıf Store'da ben fiyata göre benim Ürünleri sıralamak bir yöntem yapmak istiyorum. kullanım Eğer bu ifade:
döndüğü Nedenputs store.products_sorted_by_price
def products_sorted_by_price
@store_products.each_value do |product|
product.sort_by{|k, v| v.value.price}
end
end
"Ürün için tanımsız yöntem 'sort_by' ..." !!
Inherits; çünkü ihtiyaç duyduğum daha çok yöntem var. Farklı fiyatlardan, min fiyatını seçen bir yönteme sahibim. Ve benim hatam, siparişi Ürün'e değil, yalnızca bunlardan birine uygulamamdı. – Rat