Professional Web Applications Themes

Swapping out an instance between blinks - Ruby

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

  1. #1

    Default 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?


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

  2. #2

    Default Re: Swapping out an instance between blinks

    On Thu, 12 Feb 2004, Gavin Kistner wrote:
     

    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.
     

    -a
    --
    (short)

    Ara.T.Howard Guest

  3. #3

    Default Re: Swapping out an instance between blinks

    Ara.T.Howard wrote: 
    >
    > class MutableTime
    > def method_missing(meth, *args, &block)
    > time.send(meth, *args, &block)
    > end
    > end[/ref]

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

  4. #4

    Default Re: Swapping out an instance between blinks

    On Fri, 13 Feb 2004, Gavin Kistner wrote:
     
    > >
    > > class MutableTime
    > > def method_missing(meth, *args, &block)
    > > time.send(meth, *args, &block)
    > > end
    > > end[/ref]
    >
    > Very tricky! (And helpful, since I thought that Time had at least 1
    > method for changing the value, but realize I was wrong :)[/ref]

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

    Ara.T.Howard Guest

Similar Threads

  1. Administrative Instance now looks like Instance Manager
    By Data_Gladiator in forum Coldfusion Server Administration
    Replies: 2
    Last Post: November 10th, 09:48 PM
  2. Second IMG not swapping
    By Falconwing13 in forum Macromedia Dynamic HTML
    Replies: 3
    Last Post: October 2nd, 04:15 AM
  3. Trying to track an instance's position relative to the stage - instance is inside an MC
    By _posted.by\(Vern\); in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: January 20th, 04:52 AM
  4. kernel compiled - boot hangs - keybd blinks
    By frankh@terra.com.br in forum Linux Setup, Configuration & Administration
    Replies: 6
    Last Post: September 9th, 12:56 AM
  5. Window blinks on resize
    By Bill Jaynes in forum FileMaker
    Replies: 1
    Last Post: July 31st, 01:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139