Proposed RCR: Object#replace

J

J. Merrill

halostatue said:
object into a strange state.
Obviously, I wasn't clear. I said "At a simplistic level, this is:" ...
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...
 
A

Austin Ziegler

halostatue said:
object into a strange state.
Obviously, I wasn't clear. I said "At a simplistic level, this is:"
... 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 =3D=3D 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 =3D {}
b =3D []
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...

That's easier than one might think. I already do a lot of this work in
Transaction::Simple.

-austin
 
J

J. Merrill

halostatue said:
That's easier than one might think. I already do a lot of this work in
Transaction::Simple.

-austin

It's not difficult, but when you didn't include it in your "how this
should work" sample code you left it unclear as to whether you _want_
existing instance vars (that don't exist in the replacing-from object)
to be removed from the receiver. Am I right that you _do_ want them to
be removed?
 
A

Austin Ziegler

It's not difficult, but when you didn't include it in your "how this
should work" sample code you left it unclear as to whether you _want_
existing instance vars (that don't exist in the replacing-from object)
to be removed from the receiver. Am I right that you _do_ want them to
be removed?

Yes.

-austin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top