Object#copy [rcr?]

Discussion in 'Ruby' started by Ara.T.Howard, May 17, 2004.

  1. Ara.T.Howard

    Ara.T.Howard Guest

    can anyone think of a good reason NOT to have this feature?

    class Object
    def copy
    Marshal::load(Marshal::dump(self))
    end
    end

    i include this is nearly all of my own code and i know others do the same
    quite often - why no method which does this?

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ===============================================================================
     
    Ara.T.Howard, May 17, 2004
    #1
    1. Advertising

  2. Ara.T.Howard wrote:
    > can anyone think of a good reason NOT to have this feature?
    >
    > class Object
    > def copy
    > Marshal::load(Marshal::dump(self))
    > end
    > end
    >
    > i include this is nearly all of my own code and i know others do the same
    > quite often - why no method which does this?


    In the past I used the very same piece of code for deep copy.
    Somewhere I changed my mind from liking it.. to not liking it.
    I consider it kludgy to deepcopynig with help of serialize,
    and its inefficient compared to custom #clone method.

    Sorry.

    --
    Simon Strandgaard
     
    Simon Strandgaard, May 17, 2004
    #2
    1. Advertising

  3. Hi,

    In message "Object#copy [rcr?]"
    on 04/05/17, "Ara.T.Howard" <> writes:

    |can anyone think of a good reason NOT to have this feature?
    |
    | class Object
    | def copy
    | Marshal::load(Marshal::dump(self))
    | end
    | end

    I think deep copy should be implemented independently from
    marshaling, which is inefficient and limited scheme for deep copy.
    The only problem is that it's difficult to implement.

    matz.
     
    Yukihiro Matsumoto, May 17, 2004
    #3
  4. Ara.T.Howard

    Ara.T.Howard Guest

    On Mon, 17 May 2004, Yukihiro Matsumoto wrote:

    > Hi,
    >
    > In message "Object#copy [rcr?]"
    > on 04/05/17, "Ara.T.Howard" <> writes:
    >
    > |can anyone think of a good reason NOT to have this feature?
    > |
    > | class Object
    > | def copy
    > | Marshal::load(Marshal::dump(self))
    > | end
    > | end
    >
    > I think deep copy should be implemented independently from
    > marshaling, which is inefficient and limited scheme for deep copy.
    > The only problem is that it's difficult to implement.
    >
    > matz.


    i see your point... perhaps an initial impl as in above, or even

    class Object
    def copy opts = {'levels'=>42}
    Marshal::load(Marshal::dump(self))
    end
    end

    where opts might later affect the semantics of #copy...

    i guess i'm more interested in the __interface__ of copying: it would be nice
    to be able to write code using

    obj.copy

    and nicer still if the impl got faster/better under the hood. it may not be
    the best but, if "everyone's doing it"... ;-)


    i understand the 'inefficient' bit above, but what exactly do you have in mind
    by 'scheme'? other that 'how deep' i can't think of other things that might
    affect a deep copy?

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | "640K ought to be enough for anybody." - Bill Gates, 1981
    ===============================================================================
     
    Ara.T.Howard, May 17, 2004
    #4
  5. Ara.T.Howard

    Ara.T.Howard Guest

    On Mon, 17 May 2004, Simon Strandgaard wrote:

    > Ara.T.Howard wrote:
    > > can anyone think of a good reason NOT to have this feature?
    > >
    > > class Object
    > > def copy
    > > Marshal::load(Marshal::dump(self))
    > > end
    > > end
    > >
    > > i include this is nearly all of my own code and i know others do the same
    > > quite often - why no method which does this?

    >
    > In the past I used the very same piece of code for deep copy.
    > Somewhere I changed my mind from liking it.. to not liking it.
    > I consider it kludgy to deepcopynig with help of serialize,
    > and its inefficient compared to custom #clone method.
    >
    > Sorry.
    >
    > --
    > Simon Strandgaard



    you are right of course: i've written many a custom #clone methods and, for
    that matter, many custom #dump methods as well - all in the name of speed. i
    simply feel that the lack of a __builtin__ method to copy objects is one of
    the very few things, like the lack of a detructor hook (Object#destroy ?),
    that consistently suprises people and gives the feel of something obviously
    'missing' from ruby. the impl is really quite moot - i'm simply advocating
    something uniform in terms of __interface__ and Object#copy seems like quite a
    reasonable one.

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | "640K ought to be enough for anybody." - Bill Gates, 1981
    ===============================================================================
     
    Ara.T.Howard, May 17, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,237
  2. Replies:
    26
    Views:
    2,121
    Roland Pibinger
    Sep 1, 2006
  3. Bill Atkins
    Replies:
    5
    Views:
    122
  4. why the lucky stiff

    [RCR] Object#blank?

    why the lucky stiff, Mar 7, 2005, in forum: Ruby
    Replies:
    9
    Views:
    114
  5. Ara.T.Howard

    [RCR] Object#inside_metaclass?

    Ara.T.Howard, May 4, 2005, in forum: Ruby
    Replies:
    55
    Views:
    492
    Yukihiro Matsumoto
    May 8, 2005
Loading...

Share This Page