G
Gavin Kistner
I'm writing a class (which I'm calling MutableTime) that is like a Time
object, but...well...mutable Changeable in all sorts of magical ways.
I'd like it to have almost all the methods of Time, so this seems like a
clear case for inheritance:
class MutableTime < Time
#...
end
However, I've run into cases where I want to modify some internal values
of the instance, and (since Time is written in C) I can't figure out
what private instance variables I may or may not have access to.
For example, I want to let the user change the year:
class MutableTime < Time
def year=(newYear)
pieces = @t.to_a
pieces[5]=newYear
newTime = Time.local(*pieces)
self = newTime # ERROR
end
end
Is there any way to, inside the method for an instance, magically swap
out that instance to be represented by a new instance?
(I just realized that I can actually do what I want by making the
newTime, calculating the difference in seconds from the current time,
and then adding that in. Which is what I'll do, but I'm still interested
in the answer to the question above.)
object, but...well...mutable Changeable in all sorts of magical ways.
I'd like it to have almost all the methods of Time, so this seems like a
clear case for inheritance:
class MutableTime < Time
#...
end
However, I've run into cases where I want to modify some internal values
of the instance, and (since Time is written in C) I can't figure out
what private instance variables I may or may not have access to.
For example, I want to let the user change the year:
class MutableTime < Time
def year=(newYear)
pieces = @t.to_a
pieces[5]=newYear
newTime = Time.local(*pieces)
self = newTime # ERROR
end
end
Is there any way to, inside the method for an instance, magically swap
out that instance to be represented by a new instance?
(I just realized that I can actually do what I want by making the
newTime, calculating the difference in seconds from the current time,
and then adding that in. Which is what I'll do, but I'm still interested
in the answer to the question above.)