Swapping out an instance between blinks


(Gavin Kistner) #1

I’m writing a class (which I’m calling MutableTime) that is like a Time
object, but…well…mutable :slight_smile: 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.)


(Ara.T.Howard) #2

Date: Thu, 12 Feb 2004 23:37:19 GMT
From: Gavin Kistner gavin@refinery.com
Newsgroups: comp.lang.ruby
Subject: Swapping out an instance between blinks

I’m writing a class (which I’m calling MutableTime) that is like a Time
object, but…well…mutable :slight_smile: 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?

class MutableTime
def initialize time = Time.now
@time
end
def method_missing(meth, *args, &block)
@time.send(meth, *args, &block)
end
def swap other
@time = other
end
end

also look at the delegate pattern in the oo library section of the pickaxe.

(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.)

-a

···

On Thu, 12 Feb 2004, Gavin Kistner wrote:

(short)


(Gavin Kistner) #3

Ara.T.Howard wrote:

Is there any way to, inside the method for an instance, magically swap
out that instance to be represented by a new instance?

class MutableTime
def method_missing(meth, *args, &block)
@time.send(meth, *args, &block)
end
end

Very tricky! (And helpful, since I thought that Time had at least 1
method for changing the value, but realize I was wrong :slight_smile:


(Ara.T.Howard) #4

i do that one alot. remember though that

(Time === MutableTime.new) == false

-a

···

On Fri, 13 Feb 2004, Gavin Kistner wrote:

Date: Fri, 13 Feb 2004 01:24:34 GMT
From: Gavin Kistner gavin@refinery.com
Newsgroups: comp.lang.ruby
Subject: Re: Swapping out an instance between blinks

Ara.T.Howard wrote:

Is there any way to, inside the method for an instance, magically swap
out that instance to be represented by a new instance?

class MutableTime
def method_missing(meth, *args, &block)
@time.send(meth, *args, &block)
end
end

Very tricky! (And helpful, since I thought that Time had at least 1
method for changing the value, but realize I was wrong :slight_smile:

ATTN: please update your address books with address below!

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
STP :: http://www.ngdc.noaa.gov/stp/
NGDC :: http://www.ngdc.noaa.gov/
NESDIS :: http://www.nesdis.noaa.gov/
NOAA :: http://www.noaa.gov/
US DOC :: http://www.commerce.gov/

The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”

/bin/sh -c ‘for l in ruby perl;do $l -e “print “\x3a\x2d\x29\x0a””;done’
===============================================================================