Skip to content

Instantly share code, notes, and snippets.

@asaaki
Created February 19, 2015 17:04
Show Gist options
  • Save asaaki/cf5db59e1165fca6c5c5 to your computer and use it in GitHub Desktop.
Save asaaki/cf5db59e1165fca6c5c5 to your computer and use it in GitHub Desktop.
FunWithStringNext
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