Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cloning into an object

3 views
Skip to first unread message

Eric Mahurin

unread,
Apr 30, 2005, 5:46:23 PM4/30/05
to
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


George Ogata

unread,
Apr 30, 2005, 6:02:31 PM4/30/05
to
Eric Mahurin <eric_m...@yahoo.com> 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.

Robert Klemme

unread,
Apr 30, 2005, 6:52:07 PM4/30/05
to

"George Ogata" <g_o...@optushome.com.au> schrieb im Newsbeitrag
news:87zmvfx...@optushome.com.au...

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

Charles Steinman

unread,
Apr 30, 2005, 10:57:02 PM4/30/05
to

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.

Eric Mahurin

unread,
Apr 30, 2005, 11:17:03 PM4/30/05
to
--- Robert Klemme <bob....@gmx.net> wrote:
>
> "George Ogata" <g_o...@optushome.com.au> schrieb im
> Newsbeitrag
> news:87zmvfx...@optushome.com.au...
> > Eric Mahurin <eric_m...@yahoo.com> 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


Thanks for the pointers. I downloaded evil.rb and tried
"become" (used dest.become(source) for the copy) and it did
exactly what I wanted.

I read a little more about become. It looks like it might be
made standard in ruby at some point. Will it have the above
functionality. Or will it do something like what I think the
smalltalk "become" does - change all references to the
destination object to point to the source object. I really
don't want the reference changes because a) it seems very
expensive (search the object space), and b) this would cause
the object id to change.

Anybody know when/if become will be made standard?

David A. Black

unread,
Apr 30, 2005, 11:22:46 PM4/30/05
to
Hi --

On Sun, 1 May 2005, Eric Mahurin wrote:

> I read a little more about become. It looks like it might be
> made standard in ruby at some point. Will it have the above
> functionality. Or will it do something like what I think the
> smalltalk "become" does - change all references to the
> destination object to point to the source object. I really
> don't want the reference changes because a) it seems very
> expensive (search the object space), and b) this would cause
> the object id to change.

I thought that was the whole point of "become". Certainly it doesn't
make sense for references to object x to persist in being reference to
object x if object x has "become" object y. (The idea of having this
in Ruby doesn't appeal to me, but that's my understanding of its
premise, anyway.)


David

--
David A. Black
dbl...@wobblini.net


nobu....@softhome.net

unread,
Apr 30, 2005, 11:26:41 PM4/30/05
to
Hi,

At Sun, 1 May 2005 12:17:03 +0900,
Eric Mahurin wrote in [ruby-talk:140630]:


> Anybody know when/if become will be made standard?

When: time_t wrap arounds (where sizeof(time_t) == 8), perhaps.
If: Matz.become(Evil) executed successfully.

--
Nobu Nakada


Eric Mahurin

unread,
May 1, 2005, 9:47:11 AM5/1/05
to
> > I read a little more about become. It looks like it might
> be
> > made standard in ruby at some point. Will it have the
> above
> > functionality. Or will it do something like what I think
> the
> > smalltalk "become" does - change all references to the
> > destination object to point to the source object. I really
> > don't want the reference changes because a) it seems very
> > expensive (search the object space), and b) this would
> cause
> > the object id to change.
>
> I thought that was the whole point of "become". Certainly it
> doesn't
> make sense for references to object x to persist in being
> reference to
> object x if object x has "become" object y. (The idea of
> having this
> in Ruby doesn't appeal to me, but that's my understanding of
> its
> premise, anyway.)


What evil.rb has works great for me. It should probably be
called "replace" like the methods in Array, Hash, and String.
evil.rb/become is just a more general form of the replace in
Array, Hash, and String.

Florian Groß

unread,
May 2, 2005, 2:33:18 AM5/2/05
to
Eric Mahurin wrote:

> What evil.rb has works great for me. It should probably be
> called "replace" like the methods in Array, Hash, and String.
> evil.rb/become is just a more general form of the replace in
> Array, Hash, and String.

I agree that "replace" would be nice, but on the other hand I think the
fact that it is not guaranteed to work for all combinations of target
and source objects would be incompatible with that name. Plus there is
border cases where .replace works differently, I think, because it calls
to_ary.

"become" has been chosen because that is what this feature is usually
called in Smalltalk.

0 new messages