Created
March 21, 2018 17:55
-
-
Save alfius/3bd51d4bc0796185a9d1f7a7841507d0 to your computer and use it in GitHub Desktop.
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
# 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