Created
February 19, 2015 17:04
-
-
Save asaaki/cf5db59e1165fca6c5c5 to your computer and use it in GitHub Desktop.
FunWithStringNext
This file contains 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
class FunWithStringNext < ActiveRecord::Base | |
def create_identifier | |
total = FunWithStringNext.count | |
# ENUMERATOR: identifier_generator.take(total).last | |
generate_identifier_by_count(total) | |
end | |
def identifier_generator | |
Enumerator.new do |yielder| | |
init = INITIAL_IDENTIFIER.dup | |
loop do | |
yielder << init.dup | |
init.next! | |
end | |
end | |
end | |
def generate_identifier_by_count(count, current = INITIAL_IDENTIFIER) | |
return current if count == 0 | |
generate_identifier_by_count(count - 1, current.next) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment