Sha256: b19325cf27d9954716ebf3c995dd444c169783af6b853c7cc7867b3d2301f8ea

Contents?: true

Size: 488 Bytes

Versions: 242

Compression:

Stored size: 488 Bytes

Contents

class Luhn
  DOUBLE = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
  DOUBLE.freeze

  def self.valid?(string)
    Luhn.new(string).valid?
  end

  def initialize(string)
    @string = string.tr(' ', '')
  end

  def checksum
    @string.
      reverse.each_char.with_index.
      reduce(0) {|sum, (c, i)| sum + (i.odd? ? DOUBLE[c.to_i] : c.to_i) }
  end

  def valid?
    clean? && (checksum % 10).zero?
  end

  def clean?
    @string.match(/^\d{2,}$/)
  end
end

module BookKeeping
  VERSION = 1
end

Version data entries

242 entries across 242 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.158 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.157 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.156 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.155 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.154 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.153 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.152 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.151 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.150 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.149 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.148 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.147 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.146 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.145 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.144 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.143 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.142 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.141 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb
trackler-2.2.1.140 tracks/ruby/exercises/luhn/.meta/solutions/luhn.rb