Skip to content

Commit

Permalink
Fixed indenting
Browse files Browse the repository at this point in the history
TextWrangler for the lose. I either need to learn Vim or buy TextMate
for the home computer.
  • Loading branch information
tamalw committed Nov 15, 2011
1 parent 4577fb0 commit daf3d16
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion init.rb
Expand Up @@ -4,7 +4,7 @@

$stdin.each do |test|
filtered_text = Luhnybin.filter(test)
begin
begin
puts filtered_text
$stdout.flush
rescue Errno::EPIPE
Expand Down
36 changes: 18 additions & 18 deletions lib/luhn.rb
@@ -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
56 changes: 28 additions & 28 deletions lib/luhnybin.rb
Expand Up @@ -2,32 +2,32 @@

class Luhnybin

def self.filter(input)
@text = input
check_for_possible_matches
filter_text
@text
end
def self.check_for_possible_matches
@matches = []
0.upto(@text.length) do |pos|
12.upto(17) do |length|
m = /\d[\d\s-]{#{length}}\d/.match(@text, pos)
next unless (14..16).include?(m.to_s.scan(/\d/).size)
next unless m.to_s.scan(/\d/).join.to_i.valid_luhn?
@matches << m.offset(0) unless m.nil?
end
end
end
def self.filter_text
@matches.each do |m|
@text[m[0], m[1]-m[0]] = @text[m[0], m[1]-m[0]].gsub(/\d/, 'X')
end
end
def self.filter(input)
@text = input
check_for_possible_matches
filter_text
@text
end
def self.check_for_possible_matches
@matches = []
0.upto(@text.length) do |pos|
12.upto(17) do |length|
m = /\d[\d\s-]{#{length}}\d/.match(@text, pos)
next unless (14..16).include?(m.to_s.scan(/\d/).size)
next unless m.to_s.scan(/\d/).join.to_i.valid_luhn?
@matches << m.offset(0) unless m.nil?
end
end
end
def self.filter_text
@matches.each do |m|
@text[m[0], m[1]-m[0]] = @text[m[0], m[1]-m[0]].gsub(/\d/, 'X')
end
end

end
end
2 changes: 1 addition & 1 deletion mask.sh
@@ -1,4 +1,4 @@
#!/bin/sh

# Call your program here instead of cat.
./init.rb
./init.rb

0 comments on commit daf3d16

Please sign in to comment.