S
Simon Strandgaard
I would like to overload the '+=' operator. But it doesn't seems to be
possible? If it isn't possible, then how to use the '+' operator and at
the same time get the instance #closed correctly ?
server> ruby a.rb
open 42
open 43
close 42
close 43
-----------------------
open 42
open 43
close 43
server> expand -t2 a.rb
class Iterator
def initialize(value)
@value = value
puts "open #{@value}"
end
def close
puts "close #{@value}"
end
def +(n)
self.class.new(@value + n)
end
end
a = Iterator.new(42)
b = a + 1
a.close
b.close
puts "-----------------------"
a = Iterator.new(42)
a += 1 # Question: How to do selfassignment ?
a.close
# 42 never gets closed!
server>
possible? If it isn't possible, then how to use the '+' operator and at
the same time get the instance #closed correctly ?
server> ruby a.rb
open 42
open 43
close 42
close 43
-----------------------
open 42
open 43
close 43
server> expand -t2 a.rb
class Iterator
def initialize(value)
@value = value
puts "open #{@value}"
end
def close
puts "close #{@value}"
end
def +(n)
self.class.new(@value + n)
end
end
a = Iterator.new(42)
b = a + 1
a.close
b.close
puts "-----------------------"
a = Iterator.new(42)
a += 1 # Question: How to do selfassignment ?
a.close
# 42 never gets closed!
server>