Skip to content

Instantly share code, notes, and snippets.

@amiel
Created March 19, 2010 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiel/338208 to your computer and use it in GitHub Desktop.
Save amiel/338208 to your computer and use it in GitHub Desktop.
module IncrementifyString
# Helps create a user friendly unique string
# For example, calling incrementify_string! repeatedly starting with
# 'string' would yield:
# 'string' => 'string1' => 'string2' => 'string3' ... 'string9' => 'string10' => 'string11' ... etc
def incrementify_string!(bar)
if bar.match(/[0-8]$/)
bar.succ! # this is faster than regex parse and to_i + 1 to_s
elsif bar.ends_with?('9')
m = bar.match(/(\d+)$/)
bar.gsub!(/\d+$/, (m[1].to_i + 1).to_s)
else
bar << '1'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment