Hal E. Fulton
11/28/2003 7:48:00 PM
Simon Strandgaard wrote:
> a = Iterator.new(42)
> a += 1 # Question: How to do selfassignment ?
> a.close
> # 42 never gets closed!
What's happening here is that += is dependent on +, yet it is still
an assignment.
a += 1 is equivalent to a=a+1; so as you can see, a new object is
created and assigned to a.
You could always implement methods like succ or add that would
change the value without changing the object's identity.
For an analogy with strings and arrays, these have a #replace
method that will change all the contents without changing the
object's identity.
Or an append can be done with << on either of these also.
But = or += (with a nontrivial expression) would create new objects:
x = "abc"
y = [1,2,3]
def x.foo
puts "The string #{self} has a singleton"
end
def y.foo
puts "The array #{self} also has a singleton"
end
x.foo # The string abc has a singleton
y.foo # The array 123 also...
x << "def"
y << [4,5,6]
x.foo # The string abcdef has a ...
y.foo # The array 123456 has...
x += "ghi"
y += [7,8,9]
x.foo # Error
y.foo # would also be an error
Does this clarify any?
Hal