On Sat, 11 Dec 2004 09:22:24 +0900
Tim Hunter <cyclists / nc.rr.com> wrote:

> Brian Schr?der wrote:
> 
> > Interesting. But this won't work for instance variables that point to
> > arrays of deep_copy-able objects. Right?
> 
> I knew you'd spot that :-) In a couple of cases I had to replace Arrays with 
> something like this:
> 
>         class Content < Array 
>             def deep_copy
>                 copy = self.class.new
>                 each do |c|
>                     copy << case
>                         when c.nil?
>                             nil
>                         when c.respond_to?(:deep_copy)
>                             c.deep_copy
>                         when c.respond_to?(:dup)
>                             c.dup
>                         else
>                             c
>                         end
>                 end
>                 return copy
>             end
>         end 
> 
> And, yes, I have to be careful not to use methods that return Array objects.
> 

Maybe it would make sense to extend the base classes Object, Array, Hash with a
deep-copy functionality. That would be something for the extensions project.

The problem here is, that we have object state that is not contained in
"visible slots" i.e. instance variables. So this would be one case, where the
proposal for a 

#each_state_slot
#assign_to_state_slot(key, value), 

extension of all ruby objects would make sense. Then a deep copy would be dead
simple. (Except for recursive structures, ...)

Note, that in my view no objects with infinite state slots exist, because they
would have to be generated from more fundamental state. E.g.

class Infinite
  def each_state_slot
    i = 0
    loop do yield(i+=1, :state) end
  end
end

would simply be a wrong implementation. Infinite in this example has no own
state.

Regards,

Brian
-- 
Brian Schr?der
http://www.brian-schroeder.de/