J
J. Merrill
halostatue said:Obviously, I wasn't clear. I said "At a simplistic level, this is:" ...object into a strange state.
This means that this is *essentially* what the operation would do, but
not necessarily the full scope of what would be needed. A more "safe"
operation would be:
class Object
def replace(other)
unless other.class == self.class
raise TypeError,
"cannot convert #{self.class} into #{other.class}"
end
other.instance_variables.each do |name|
instance_variable_set(name,
other.instance_variable_get(name))
end
end
end
This is, by the way, exactly what:
a = {}
b = []
b.replace a
does. [snip]
What if the receiver of #replace has instance variables that don't exist
in the parameter? Do you intend for them to go away? If they don't,
you could break code that uses
if @instancevar ...
Doing so would require getting both sets of instance variables etc...