Skip to content

Instantly share code, notes, and snippets.

@amiel
Created April 18, 2009 01:52
Show Gist options
  • Save amiel/97387 to your computer and use it in GitHub Desktop.
Save amiel/97387 to your computer and use it in GitHub Desktop.
module IncrementifyString
# Helps create a user friendly unique string
# For example, calling incrementify! repeatedly starting with
# 'string' would yield:
# 'string' => 'string1' => 'string2' => 'string3' ... 'string9' => 'string10' => 'string11' ... etc
def incrementify!
if match(/[0-8]$/)
succ! # this is faster than regex parse and to_i + 1 to_s
elsif ends_with?('9')
m = match(/(\d+)$/)
gsub!(/\d+$/, (m[1].to_i + 1).to_s)
else
self << '1'
end
end
end
String.send :include, IncrementifyString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment