Created
April 18, 2009 01:52
-
-
Save amiel/97387 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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