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
baz

puts “baz”.next
bba

puts “bzz”.next
caa

puts “zzz”.next
aaaa

puts “ZZZ”.next
AAAA

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.”
http://www.ruby-doc.org/core-2.0/String.html#method-i-next

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

Thanks!

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]
wrote:

Well, I’ve heard of exploding penguins
https://www.youtube.com/watch?feature=player_detailpage&v=1k1ccguXiws&t=148
but this is a first or maybe that was the explosion