Skip to content

Instantly share code, notes, and snippets.

@Cohen-Carlisle
Created April 9, 2015 01:08
Show Gist options
  • Save Cohen-Carlisle/e0878c1a096c8a49f148 to your computer and use it in GitHub Desktop.
Save Cohen-Carlisle/e0878c1a096c8a49f148 to your computer and use it in GitHub Desktop.
Barbaric #succ
###############################################################################
# #
# Returns the successor to str. The successor is calculated by incrementing #
# characters starting from the rightmost alphanumeric (or the rightmost #
# character if there are no alphanumerics) in the string. Incrementing a #
# digit always results in another digit, and incrementing a letter results in #
# another letter of the same case. Incrementing nonalphanumerics uses the #
# underlying character set’s collating sequence. #
# #
# 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. #
# #
# "abcd".succ #=> "abce" #
# "THX1138".succ #=> "THX1139" #
# "<<koala>>".succ #=> "<<koalb>>" #
# "1999zzz".succ #=> "2000aaa" #
# "ZZZ9999".succ #=> "AAAA0000" #
# "***".succ #=> "**+" #
# #
###############################################################################
"succ".succ
"succ".upcase.succ
"9000".succ
"codez".succ
"999".succ
"zzz".succ
"Zzz".succ
" ".succ
"\u007F".succ
"succ ".succ
"a9".succ
"0z".succ
"09".succ
"az".succ
"a 9".succ
"0 z".succ
"0 9".succ
"a z".succ
c="\u0000";90.times{c.succ!};c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment