M
Michael W. Ryder
But i.succ does Not work in the following:Walton said:-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]] On Behalf Of Tony
Arcieri
wrote:
I think you're missing why ++ could be useful, and it's precisely
because
Ruby is a "21st century language"
The ++ operator, far more than just being syntactic sugar for +=1,
would
allow you to send an "increment" message to any object, which would
change
its value in place, i.e.
def ++
incrementing_logic_goes_here
end
I could see this as being handy
But you already can with the mechanics of the language that are already
present!
irb(main):003:0> i=15
=> 15
irb(main):004:0> i=i.succ
=> 16
irb(main):005:0> i="15"
=> "15"
irb(main):006:0> i=i.succ
=> "16"
irb(main):007:0> i=1.2
=> 1.2
irb(main):008:0> i=i.succ
NoMethodError: undefined method `succ' for 1.2:Float
from (irb):8
from /usr/local/bin/irb:12:in `<main>'
In an object that it makes sense to increment, define the #succ method!
It's that easy!
i = 1
while (i < 10)
puts i.succ
end
the only way to get this to work is to use:
puts i; i = i.succ
which is not as clean as using puts i++.