Created
March 19, 2010 21:26
-
-
Save amiel/338208 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_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