Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Cloning into an object

Reply
Thread Tools

Cloning into an object

 
 
Eric Mahurin
Guest
Posts: n/a
 
      04-30-2005
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


 
Reply With Quote
 
 
 
 
George Ogata
Guest
Posts: n/a
 
      04-30-2005
Eric Mahurin <(E-Mail Removed)> 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.



 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-30-2005

"George Ogata" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Eric Mahurin <(E-Mail Removed)> 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

 
Reply With Quote
 
Charles Steinman
Guest
Posts: n/a
 
      05-01-2005

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cloning object with array members Shea Martin Ruby 7 01-09-2007 09:52 PM
deep cloning an object via interfaces? Elhanan Java 0 12-27-2006 06:46 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Re: Cloning String object Will Clark Java 0 08-23-2003 04:12 PM
Re: Cloning String object Marco Schmidt Java 0 08-23-2003 04:10 PM



Advertisments