What does "self" refer to when it's inside an "if" statement (or is it called block?) that is inside an instance method?
I'm referring to my code in the last method: "apply_discount"
I thought it'd get me the @total and @discount instance variables, but it's not doing so.
So, basically, how would I get these variables from the my place in the "if" statement I'm referring to by using "self"?
Or should I use another way?
class CashRegister
attr_accessor :total, :discount
def initialize(discount = 0)
@total = 0
@discount = discount
end
def add_item(title, price, quantity = 0)
if quantity > 0
self.total += (price * quantity)
else
self.total += price
end
end
def apply_discount
# total = self.total
# discount = self.discount
if self.discount > 0
self.total = self.total - (self.total * (self.discount / 100.0))
"After the discount, the total comes to $#{self.total}."
else
"There is no discount to apply."
end
end
end
Thanks for your help!
Aucun commentaire:
Enregistrer un commentaire