オブジェクトクラス内のメソッドを使用してオブジェクトを作成した後で、オブジェクトに属性を追加しようとしています。このコードをdef set_sell_byとdef get_sell_byメソッドに入れたいと思います。ですから、最後に、apple.set_sell_by(10)を実行して、後でapple.get_sell_byを実行して、そのアイテムが5日以内にそれを販売するかどうかを確認して、その値を取得したいと思います。メソッド(Ruby)を使用してオブジェクトを作成した後に新しい属性を追加する
class Grocery_Inventory
\t attr_accessor :product, :store_buy, :quantity, :serial_number, :customer_buy
\t def initialize(product, store_buy, quantity, serial_number, customer_buy)
\t \t @product = product
\t \t @store_buy = store_buy
\t \t @quantity = quantity + 5
\t \t @serial_number = serial_number
\t \t @customer_buy = customer_buy
\t end
\t
\t def get_product_name
\t \t p product
\t \t self
\t end
\t
\t def get_cost_customer
\t \t p "$#{customer_buy}"
\t \t self
\t end
\t def get_product_quantity
\t \t p "You have #{quantity} #{product}"
\t \t self
\t end
\t def set_sell_by
\t \t #some code...
\t \t self
\t end
\t def get_sell_by
\t \t if sell_by < 5
\t \t \t p "You need to sell this item within five days."
\t \t \t self
\t \t else
\t \t \t p "Item doesn't currently need to be sold."
\t \t \t self
\t \t end
\t end
end
apples = Grocery_Inventory.new("apples", 1.00, 5, 123, 0.25)
apples.get_product_name
apples.get_cost_customer
apples.get_product_quantity
あなたはすでに 'initialize'メソッドであなたが尋ねていることを*正確に*行います。だから、明らかに*可能です。また、あなたはそれを試して何が起こったのですか? –