Last active
October 4, 2018 16:41
-
-
Save tmikeschu/284d0c766fd3dbc34934afa265d0db85 to your computer and use it in GitHub Desktop.
ruby luhn iteration 1
This file contains hidden or 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
class Luhn | |
class << self | |
VALIDATORS = %i(valid_length? only_digits? valid_sum?) | |
def valid?(raw) | |
VALIDATORS.all? { |predicate| send(predicate, raw.tr(" ", "")) } | |
end | |
private | |
def valid_length?(raw) | |
raw.length > 1 | |
end | |
def only_digits?(raw) | |
!/[^\d^\s]/.===(raw) | |
end | |
def valid_sum?(raw) | |
luhn_sum(raw) % 10 == 0 | |
end | |
def luhn_sum(raw) | |
raw. | |
chars. | |
reverse. | |
map.with_index(&luhn_value). | |
sum(&luhn_doubled) | |
end | |
def luhn_value | |
-> x, index { x.to_i * (index % 2 + 1) } | |
end | |
def luhn_doubled | |
-> x { x - (9 * (x / 10)) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment