Created
July 26, 2012 12:57
-
-
Save jamiefdhurst/3181897 to your computer and use it in GitHub Desktop.
This is me learning "Ruby"...
This file contains hidden or 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
| #!/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 In:
def remove_non_digits
isbn = self.scan(/\d/).join('')
end
No need to do isbn = as you're just returning it.
fair point. Updated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does the same thing and is hopefully a bit more Ruby-ish.
Hope it's useful..