Skip to content

Instantly share code, notes, and snippets.

@jamiefdhurst
Created July 26, 2012 12:57
Show Gist options
  • Select an option

  • Save jamiefdhurst/3181897 to your computer and use it in GitHub Desktop.

Select an option

Save jamiefdhurst/3181897 to your computer and use it in GitHub Desktop.
This is me learning "Ruby"...
#!/usr/bin/env ruby
def is_valid_isbn13?(isbn13)
sum = 0
13.times { |i| sum += i % 2 == 0 ? isbn13[i].to_i : isbn13[i].to_i * 3 }
0 == sum % 10
end
puts "Loading file and reading ISBNs..."
isbns = []
File.open("isbns.txt", "r").each_line do |line|
isbns.push(line)
end
puts "Cleaning ISBNs..."
isbns.collect! do |isbn|
isbn = isbn.scan(/\d/).join('')
end
puts "Checking ISBNs..."
isbns.each do |isbn|
sum = 0
if (isbn.length != 13 or is_valid_isbn13?(isbn) == false)
puts " - #{isbn} is invalid..."
isbns.delete(isbn)
else
puts " - #{isbn} is valid..."
end
end
@richquick
Copy link

fair point. Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment