Skip to content

Instantly share code, notes, and snippets.

@alfius
Created March 21, 2018 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfius/3bd51d4bc0796185a9d1f7a7841507d0 to your computer and use it in GitHub Desktop.
Save alfius/3bd51d4bc0796185a9d1f7a7841507d0 to your computer and use it in GitHub Desktop.
# https://stackoverflow.com/a/29299786/512507
def self.valid_id?(id)
case id
when /^[a-zA-Z0-9]{15}$/
return true
when /^[a-zA-Z0-9]{18}$/
id[15..17] == [
id[0..4],
id[5..9],
id[10..14]
].map(&:reverse).
map do |p|
(('A'..'Z').to_a + ('0'..'9').to_a)[
p.chars.map { |c| c =~ /[A-Z]/ ? '1' : '0' }.join.to_i(2)
]
end.join
else
return false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment