Class: LuhnBase
- Inherits:
-
Object
- Object
- LuhnBase
- Defined in:
- lib/brid/algorithms/luhn.rb
Instance Method Summary collapse
- #*(digits) ⇒ Object
- #+(digit) ⇒ Object
- #==(other) ⇒ Object
- #check_digit ⇒ Object
-
#initialize(number, options = {}) ⇒ LuhnBase
constructor
A new instance of LuhnBase.
- #to_s ⇒ Object
Constructor Details
#initialize(number, options = {}) ⇒ LuhnBase
Returns a new instance of LuhnBase.
2 3 4 5 6 7 |
# File 'lib/brid/algorithms/luhn.rb', line 2 def initialize number, = {} @mod, @base = 0, 0..100 @number = number.to_s.scan(/\d/).join @base = [:base] if .has_key? :base @mod = [:mod] if .has_key? :mod end |
Instance Method Details
#*(digits) ⇒ Object
13 14 15 16 17 |
# File 'lib/brid/algorithms/luhn.rb', line 13 def * digits digits.times.reduce(self) do |number, digit| number += self.class.new(number, :base => @base, :mode => @mod).check_digit end end |
#+(digit) ⇒ Object
19 20 21 |
# File 'lib/brid/algorithms/luhn.rb', line 19 def + digit self.class.new(@number += digit.to_s.scan(/\d/).join, :base => @base, :mode => @mod) end |
#==(other) ⇒ Object
27 28 29 |
# File 'lib/brid/algorithms/luhn.rb', line 27 def == other self.to_s == other.to_s end |
#check_digit ⇒ Object
9 10 11 |
# File 'lib/brid/algorithms/luhn.rb', line 9 def check_digit sum / @mod end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/brid/algorithms/luhn.rb', line 23 def to_s @number.to_s end |