Sha256: 965d065e67e7657a49f90cafb9699a136ea1927056e56c4ad172571df48da514
Contents?: true
Size: 866 Bytes
Versions: 2
Compression:
Stored size: 866 Bytes
Contents
require "turkish_id/version" class TurkishId def initialize(id_number) begin # Convert string into array of integers @id_number = id_number.split('').map { |s| Integer(s) } rescue @id_number = '' # Suppress error & return false (eventually) end end def is_valid? id = @id_number # +1 brevity point # Early elimination, bad length or first digit return false if id.length != 11 || id.first == 0 # Calculate the sums of odd and even digits odds = id.values_at(0, 2, 4, 6, 8).reduce(:+) evens = id.values_at(1, 3, 5, 7).reduce(:+) # Check if the tenth digit matches the algorithm return false if id[9] != ((odds * 7) - evens) % 10 # Check if the eleventh digit matches the algorithm return false if id[10] != id[0..9].reduce(:+) % 10 return true # All conditions met end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
turkish_id-0.2.0 | lib/turkish_id.rb |
turkish_id-0.1.0 | lib/turkish_id.rb |