Why does the next() method behave this way?

I might be getting tripped up on something not important, but hopefully
one of y’all can help me understand this :slight_smile:

What is a use case where you’d want next() to behave the way it does?

See this from irb:

puts “bay”.next

puts “baz”.next

puts “bzz”.next

puts “zzz”.next

puts “ZZZ”.next

The manual says this is because:
“If the increment generates a “carry,” the character to the left of it
is incremented. This process repeats until there is no carry, adding an
additional character if necessary.”

Come again? What exactly causes an increment to generate a carry? Is
only z to a and Z to A? Why?


Think of it as mathematics:
z is 9
when you do z.next which is equivalent to 9+1 which results in 1 carry
to the digit the the left.

So think of baz as 219
baz.next => 219+1 = 220
so in this case z will be incremented to a,
a will get the carry from z and increment to b.

Hope that helps.

On Aug 4, 2013, at 11:32 PM, Suicidal P. [email protected]

Well, I’ve heard of exploding penguins
but this is a first or maybe that was the explosion