Skip to content

Instantly share code, notes, and snippets.

@ryenski
Created December 12, 2022 23:58
Show Gist options
  • Save ryenski/3ccc58405ff40170b7230b16bec9b0ae to your computer and use it in GitHub Desktop.
Save ryenski/3ccc58405ff40170b7230b16bec9b0ae to your computer and use it in GitHub Desktop.
A small script to generate memorable one-time passcodes.
# A small script to generate memorable one-time codes.
#
# OneTimeCode.generate
# => "ubahow-idea-easy-aveda"
class OneTimeCode
def initialize(length = 12)
@length = length
@mod_result = MOD_RESULT.sample
end
def generate
length.times.map { |i| i % 2 == mod_result ? CONSONANTS.sample : VOGALS.sample }.join
end
private
VOGALS = %w[a e i o u y].freeze
CONSONANTS = [*'a'..'z'] - VOGALS
MOD_RESULT = [0, 1].freeze
WORDS = %w[airy ally bold boss calm care cool core cure cute each easy fair fast feat fine free full give glad glow good grin grow here hold holy idea keen kind kiss know love].freeze
attr_reader :length, :mod_result
def self.generate
words = 2.times.map do
new(rand(4..6)).generate
end
words.shuffle!
2.times { words.prepend(WORDS.sample) }
words.shuffle.join('-')
end
end
OneTimeCode.generate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment