Module: AbaNumbers::Validation

Defined in:
lib/aba_numbers/validation.rb

Class Method Summary collapse

Class Method Details

.valid_aba_number?(aba_number) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/aba_numbers/validation.rb', line 4

def valid_aba_number?(aba_number)
  aba_number_str = aba_number.to_s.strip

  return false if aba_number_str.empty?
  return false unless aba_number_str =~ /^\d{9}$/

  total = 0
  0.step(9, 3) do |i|
    total += aba_number_str[i..i].to_i * 3
    total += aba_number_str[(i+1)..(i+1)].to_i * 7
    total += aba_number_str[(i+2)..(i+2)].to_i * 1
  end

  total % 10 == 0
end