Swapping out an instance between blinks

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

Ara.T.Howard

Date: Thu, 12 Feb 2004 23:37:19 GMT
From: Gavin Kistner <[email protected]>
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 :) 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
 
G

Gavin Kistner

Ara.T.Howard said:
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 :)
 
A

Ara.T.Howard

Date: Fri, 13 Feb 2004 01:24:34 GMT
From: Gavin Kistner <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: Swapping out an instance between blinks



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

i do that one alot. remember though that

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

-a

--

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'
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top