A
Austin Ziegler
On the Sydney list, Daniel Berger has been asking about a generic
Object#replace call to work similarly to Hash#replace, String#replace,
and Array#replace. I see no reason that this could not be implemented
for all objects. At a simplistic level, this is:
class Object
def replace(other)
other.instance_variables.each do |name|
instance_variable_set(name, other.instance_variable_get(name))
end
end
end
It doesn't necessarily work on C-based objects which may have "hidden"
state (as the Hash, String, and Array objects do), but that can be
implemented in a C-callback.
I have other thoughts about this, including how one might consider
replacing a graph so that Transaction::Simple doesn't have object
growth on large object graphs, but that's barely formed in my head
right now, and I thought that a discussion on the pros and cons of
having something like this -- so that I can replace an object with the
contents of another object without losing the first object's
#object_id.
-austin
Object#replace call to work similarly to Hash#replace, String#replace,
and Array#replace. I see no reason that this could not be implemented
for all objects. At a simplistic level, this is:
class Object
def replace(other)
other.instance_variables.each do |name|
instance_variable_set(name, other.instance_variable_get(name))
end
end
end
It doesn't necessarily work on C-based objects which may have "hidden"
state (as the Hash, String, and Array objects do), but that can be
implemented in a C-callback.
I have other thoughts about this, including how one might consider
replacing a graph so that Transaction::Simple doesn't have object
growth on large object graphs, but that's barely formed in my head
right now, and I thought that a discussion on the pros and cons of
having something like this -- so that I can replace an object with the
contents of another object without losing the first object's
#object_id.
-austin