ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: Cloning into an object
by Eric Mahurin other posts by this author
Apr 30 2005 8:23PM messages near this date
Re: OpenGL 3D gears demo for Ruby | Re: Cloning into an object
--- Robert Klemme <bob.news@[...].net>  wrote:
>  
>  "George Ogata" <g_ogata@[...].au> schrieb im
>  Newsbeitrag 
>  news:87zmvfx6u9.fsf@[...]..
>  > Eric Mahurin <eric_mahurin@[...].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?


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
Thread:
Eric Mahurin
Nobu Nokada
David A. Black

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved