forked from tamalw/luhnybin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TextWrangler for the lose. I either need to learn Vim or buy TextMate for the home computer.
- Loading branch information
Showing
4 changed files
with
48 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
class Integer | ||
def valid_luhn? | ||
digits = [] | ||
self.to_s.reverse.chars.each_with_index do |digit, i| | ||
if i % 2 != 0 | ||
double_digit = (digit.to_i * 2).to_s | ||
if double_digit.size > 1 | ||
double_digit = double_digit[0].to_i + double_digit[1].to_i | ||
end | ||
digits << double_digit | ||
else | ||
digits << digit | ||
end | ||
end | ||
digits.inject(0) { |sum, n| sum + n.to_i } % 10 == 0 | ||
end | ||
def valid_luhn? | ||
digits = [] | ||
self.to_s.reverse.chars.each_with_index do |digit, i| | ||
if i % 2 != 0 | ||
double_digit = (digit.to_i * 2).to_s | ||
if double_digit.size > 1 | ||
double_digit = double_digit[0].to_i + double_digit[1].to_i | ||
end | ||
digits << double_digit | ||
else | ||
digits << digit | ||
end | ||
end | ||
digits.inject(0) { |sum, n| sum + n.to_i } % 10 == 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/sh | ||
|
||
# Call your program here instead of cat. | ||
./init.rb | ||
./init.rb |