Reputation: 315
For example, I have:
class Camel
attr_accessor :name, :meat_quality
def initialize(name, meat_quality)
name = @name
meat_quality = @meat_quality
end
end
And I initialize an instance carlos
, and later change its @meat_quality
value:
carlos = Camel.new("Carlos","orphan grade")
...
carlos.meat_quality = "public school grade"
Later, though, I need a way to refer to whatever was passed to @meat_quality
when carlos
was first initialized (namely "tough and stringy"
).
I will be changing @meat_quality
multiple times, so I can't just use default values or make a variable like previous_quality
.
Is this possible?
Upvotes: 0
Views: 663
Reputation: 29419
There's nothing in Ruby that saves the initial value passed on, so nothing in Ruby that will let you automatically reset it. However, you can certainly save the value in an another instance variable and reset it as you wish, as in:
def initialize(..., meat_quality)
...
@original_meat_quality = @meat_quality = meat_quality
end
def reset_meat_quality
@meat_quality = @original_meat_quality
end
Upvotes: 2