I’d like to add a method to Ruby’s Object to reassigns the value associated with it.
For instance, if I had two strings, I could call my method on one of them so that the pointer to the underlying char[] would point to the other’s underlying char[].
a = object_a
b = object_b
a == object_b #=> false
a.my_method(object_b)
a == object_b #=> true
Can anyone see a way to accomplish this?
You can’t change
self, it always points to current object. You can, however, make a proxy around the object. This is quite simple in ruby usingSimpleDelegatorfrom the standard library:You can alias
__setobj__to something nicer, for example: