Skip to content

Instantly share code, notes, and snippets.

@bmatzelle
Created May 2, 2013 14:13
Show Gist options
  • Save bmatzelle/5502476 to your computer and use it in GitHub Desktop.
Save bmatzelle/5502476 to your computer and use it in GitHub Desktop.
Ruby routing number check sum function
# Ruby routing number check sum function. Code adapted from Wikipedia Python
# example: http://en.wikipedia.org/wiki/Routing_transit_number
# Returns true if a routing number string check sum is valid.
def routing_number_check_sum(number)
d = []
number.each_char { |char| d << char.to_i }
d[8] == (7 * (d[0] + d[3] + d[6]) +
3 * (d[1] + d[4] + d[7]) +
9 * (d[2] + d[5])
) % 10
end
# List of valid routing numbers
list = %w(211170363 065000090 065001426 065002030 065002548 111904451 065200243)
list.each do |number|
puts "#{number} is valid: #{routing_number_check_sum(number)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment