Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string_validator.rb

Overview

Extends the String class in order to validate UC Student IDs.

Instance Method Summary collapse

Instance Method Details

#valid_uc_number?Boolean

Validation method.

Example:

>> '11533513'.valid_uc_number?
=> false

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
# File 'lib/string_validator.rb', line 8

def valid_uc_number?
  last_num = self[-1, 1].upcase
  rest_num = reverse[1..-1].split('').map(&:to_i)

  sum = 0
  rest_num.each_with_index { |digit, index| sum += digit * ((index + 2) % 9) }

  last_num == ((11 - sum % 11 != 10) ? (11 - sum % 11).to_s : 'J')
end