Class: CCheck::Luhn
- Inherits:
-
Object
- Object
- CCheck::Luhn
- Defined in:
- lib/ccheck/luhn.rb
Instance Attribute Summary collapse
-
#card ⇒ Object
readonly
Returns the value of attribute card.
-
#digits ⇒ Object
readonly
Returns the value of attribute digits.
Instance Method Summary collapse
-
#initialize(card) ⇒ Luhn
constructor
Sets @card and also builds its luhn @digits.
-
#valid? ⇒ Boolean
Public: Verifies that the given number is a valid CC.
Constructor Details
#initialize(card) ⇒ Luhn
Sets @card and also builds its luhn @digits.
card - CC String
10 11 12 13 |
# File 'lib/ccheck/luhn.rb', line 10 def initialize card @card = card @digits = luhn_digits end |
Instance Attribute Details
#card ⇒ Object (readonly)
Returns the value of attribute card.
4 5 6 |
# File 'lib/ccheck/luhn.rb', line 4 def card @card end |
#digits ⇒ Object (readonly)
Returns the value of attribute digits.
4 5 6 |
# File 'lib/ccheck/luhn.rb', line 4 def digits @digits end |
Instance Method Details
#valid? ⇒ Boolean
Public: Verifies that the given number is a valid CC. Basically the third step of the Luhn algorighthm. If the result is a multiple of 10, the number is valid.
Returns Boolean
20 21 22 23 24 |
# File 'lib/ccheck/luhn.rb', line 20 def valid? digits.split('').inject(0) do |sum, digit| sum + digit.to_i end % 10 == 0 end |