From: Austin Ziegler Date: 2005-11-17T08:41:51+09:00 Subject: Re: Proposed RCR: Object#replace On 11/16/05, Eric Mahurin wrote: > On 11/16/05, Austin Ziegler wrote: >> This is, by the way, exactly what: >> a = {} >> b = [] >> b.replace a >> >> does. Yes, I think it should be that restrictive -- exact match only. >> This isn't #become, it's #replace. This isn't typecasting, it's >> replacing the internals of one object with those of another object OF >> THE EXACT SAME KIND. (Granted, singleton objects tend to confuse >> things, but this is closer to the behaviour of #dup than of #clone.) >> The behaviour for this is relatively well-established by the #replace >> implementations on the internal objects Array, Hash, and String. > Definitely safer. But, like you said objects with their own meta > class do confuse things. Not really; I addressed that in my first omnibus response. a = {} class << a def x; end end a.x b = {} b.x # NoMethodError b.replace a b.x # NoMethodError a.replace b a.x I am suggesting that we implement the same sort of behaviour that we see in Hash#replace. > But, it still doesn't seem very encapsulated because it is using > instance_variable_get on other. It's the best way to do it. It's not perfect, actually, because it's only getting instance variables that are currently in self. It would need to possibly be a bit more robust (pure pseudocode here): class Object def replace(other) # check to make sure that we claim to be the same thing # copy all instance variables from other # delete instance variables in self that aren't in other # return self end end Honestly, for the purposes that *I* need this, I have to have things that don't have pure correspondence to attribute accessors -- and we don't really care whether or not we go through accessors in the least, because we're pulling state from the other object. I do a lot of this already (including the instance variable deletion) in Transaction::Simple. What I *can't* do -- and this is a larger problem than just Object#replace, but this helps me get a little closer -- is deal with graphs properly after they're marshaled. Right now, I look for an explicit response to #replace and use that if it's available. I'd *like* to be able to just use #replace and know that it's safe. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca