Following on from my JavaScript NHS Number validator, here it is converted to Ruby on Rails:
class NhsNumberValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) isValid = false if value.length == 10 total = 0; i = 0; 9.times { digit = value[i] factor = 10 - i total += (Integer(digit) * factor) i += 1 } checkDigit = (11 - (total % 11)) checkDigit = 0 if checkDigit == 11 isValid = true if checkDigit == Integer(value[9]) end record.errors[attribute] << "is not valid" unless isValid end end