Skip to content

Instantly share code, notes, and snippets.

@amiel

amiel/gist:97387

Created Apr 18, 2009
Embed
What would you like to do?
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
You can’t perform that action at this time.