Cloning into an object

Discussion in 'Ruby' started by Eric Mahurin, Apr 30, 2005.

  1. Eric Mahurin

    Eric Mahurin Guest

    Anybody know how to generically copy (shallow like clone) an
    object into another object? It would be nice to handle the
    case where the objects have a different class, but I would at
    least like to know how to do it when the classes are the same.

    Here is an example of what I would like to do:

    dest = Object.new # or String.new if necessary
    source = "hello world"
    destid = dest.id

    source.clone_into(dest)

    dest -> "hello world"
    dest.id==destid -> true
    dest.class -> String



    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
    Eric Mahurin, Apr 30, 2005
    #1
    1. Advertising

  2. Eric Mahurin

    George Ogata Guest

    Eric Mahurin <> writes:

    > Anybody know how to generically copy (shallow like clone) an
    > object into another object? It would be nice to handle the
    > case where the objects have a different class, but I would at
    > least like to know how to do it when the classes are the same.
    >
    > Here is an example of what I would like to do:
    >
    > dest = Object.new # or String.new if necessary
    > source = "hello world"
    > destid = dest.id
    >
    > source.clone_into(dest)
    >
    > dest -> "hello world"
    > dest.id==destid -> true
    > dest.class -> String


    I hear that evil.rb has Object#become, which will do that with some
    caveats. Search "Object#become" in the list archives for more info.
    George Ogata, Apr 30, 2005
    #2
    1. Advertising

  3. "George Ogata" <> schrieb im Newsbeitrag
    news:...
    > Eric Mahurin <> writes:
    >
    >> Anybody know how to generically copy (shallow like clone) an
    >> object into another object? It would be nice to handle the
    >> case where the objects have a different class, but I would at
    >> least like to know how to do it when the classes are the same.
    >>
    >> Here is an example of what I would like to do:
    >>
    >> dest = Object.new # or String.new if necessary
    >> source = "hello world"
    >> destid = dest.id
    >>
    >> source.clone_into(dest)
    >>
    >> dest -> "hello world"
    >> dest.id==destid -> true
    >> dest.class -> String

    >
    > I hear that evil.rb has Object#become, which will do that with some
    > caveats. Search "Object#become" in the list archives for more info.


    If you just want to copy state you can do without evil magic (although not
    working for builtins like String, Array, Fixnum etc.):

    class Object
    def set_from(o)
    o.instance_variables.each do |var|
    instance_variable_set( var, o.instance_variable_get( var ) )
    end
    self
    end
    end

    Kind regards

    robert
    Robert Klemme, Apr 30, 2005
    #3
  4. Eric Mahurin wrote:
    > Anybody know how to generically copy (shallow like clone) an
    > object into another object? It would be nice to handle the
    > case where the objects have a different class, but I would at
    > least like to know how to do it when the classes are the same.
    >
    > Here is an example of what I would like to do:
    >
    > dest = Object.new # or String.new if necessary
    > source = "hello world"
    > destid = dest.id
    >
    > source.clone_into(dest)
    >
    > dest -> "hello world"
    > dest.id==destid -> true
    > dest.class -> String


    Is the only difference between this and Object#clone that the object_id
    is the same as the old object's? Because that's all I can see here, but
    I don't understand why that would come in handy.
    Charles Steinman, May 1, 2005
    #4
    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. Guest

    Cloning Possible?

    Guest, Nov 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    392
    Steve C. Orr [MVP, MCSD]
    Nov 11, 2003
  2. Marco Schmidt

    Re: Cloning String object

    Marco Schmidt, Aug 23, 2003, in forum: Java
    Replies:
    0
    Views:
    367
    Marco Schmidt
    Aug 23, 2003
  3. Will Clark

    Re: Cloning String object

    Will Clark, Aug 23, 2003, in forum: Java
    Replies:
    0
    Views:
    6,036
    Will Clark
    Aug 23, 2003
  4. Elhanan
    Replies:
    0
    Views:
    288
    Elhanan
    Dec 27, 2006
  5. Shea Martin

    cloning object with array members

    Shea Martin, Jan 5, 2007, in forum: Ruby
    Replies:
    7
    Views:
    126
    Stefano Crocco
    Jan 9, 2007
Loading...

Share This Page