# I took your philosophy of method chaining to its logic conclusion, # removing the local variable `sum` from the `valid?` method. # I was able to remove both instances of .map { |char| char.to_i } # by doing the the integer conversion in the subsequent step, saving # both time and space. I also reversed the conditional statement in # `maybe_double`, since I think it makes it clearer when it doubles. def valid?(card_number) card_number # => "4929735477250543" .reverse # => "3450527745379294" .chars # => ["3", "4", "5", "0", "5", "2", "7", "7", "4", "5", "3", "7", "9", "2", "9", "4"] .map.with_index { |n, i| maybe_double(n.to_i, i) } # => [3, 8, 5, 0, 5, 4, 7, 14, 4, 10, 3, 14, 9, 4, 9, 8] .inject { |s, n| s + sum_digits(n) } # => 80 .modulo(10) # => 0 .zero? # => true end def sum_digits(number) number # => 3, 8, 5, 0, 5, 4, 7, 14, 4, 10, 3, 14, 9, 4, 9, 8 .to_s # => "3", "8", "5", "0", "5", "4", "7", "14", "4", "10", "3", "14", "9", "4", "9", "8" .chars # => ["3"], ["8"], ["5"], ["0"], ["5"], ["4"], ["7"], ["1", "4"], ["4"], ["1", "0"], ["3"], ["1", "4"], ["9"], ["4"], ["9"], ["8"] .inject(0) { |s, n| s + n.to_i } # => 3, 8, 5, 0, 5, 4, 7, 5, 4, 1, 3, 5, 9, 4, 9, 8 end def maybe_double(number, index) if index.odd? number * 2 else number end end # # valid # ['79927398713', '5541808923795240', '4024007136512380', '6011797668867828'] # .map { |num| valid? num } # => [true, true, true, true] # # # invalid # ['5541801923795240', '4024007106512380', '6011797668868728'] # .map { |num| valid? num } # => [false, false, false] # process the example if valid? "4929735477250543" puts "The number is valid!" else puts "The number is invalid!" end # >> The number is valid!