Skip to content

Instantly share code, notes, and snippets.

@chandeeland
Created September 14, 2018 19:50
Show Gist options
  • Save chandeeland/f3a36b1355b6de21d58675ad45f421e9 to your computer and use it in GitHub Desktop.
Save chandeeland/f3a36b1355b6de21d58675ad45f421e9 to your computer and use it in GitHub Desktop.
check number
class CheckNumber
ALPHABET = (2..9).to_a + ('a'..'z').to_a - %w[o i]
# ALPHABET = (0..9).to_a + ('a'..'z').to_a + ('A'..'Z').to_a + %w[ @ & ]
def initialize(uuid, size = 4, chunk_size = 4)
@uuid = uuid
@size = size
@chunk_size = chunk_size
end
def run
return chunks if size != chunk_size
chars.join
end
private
attr_reader :uuid, :size, :chunk_size
def chars
num = uuid.delete('-').to_i(16)
Array.new(size).map.with_index do |elem, i|
curr = ALPHABET.size
ALPHABET[num % (curr).to_i].tap do
num /= curr
end
end
end
def chunks
chars
.each_slice( chunk_size )
.map(&:join)
.join(' ')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment