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 |
This does the same thing and is hopefully a bit more Ruby-ish.
Hope it's useful..
#!/usr/bin/env ruby
class String
def remove_non_digits
self.scan(/\d/).join('')
end
def remove_non_digits!
replace remove_non_digits
end
def is_valid_isbn13?
isbn13 = self.remove_non_digits!
sum = 0
13.times { |i| sum += i % 2 == 0 ? isbn13[i].to_i : isbn13[i].to_i * 3 }
0 == sum % 10
end
end
File.open("isbns.txt", "r").each do |potential_isbn|
if potential_isbn.is_valid_isbn13?
message = " is a valid ISBN13"
else
message = " is not a valid ISBN 13"
end
puts potential_isbn + message
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
If you're struggling with that first method, maybe this helps:
I guess you're struggling with this line:
13.times { |i| sum += i % 2 == 0 ? isbn13[i].to_i : isbn13[i].to_i * 3 }13.times {}means to execute the block every time, and|i|denotesito be the counter. Within the block you've got a ternary operation:sum += i % 2 == 0 ? isbn13[i].to_i : isbn13[i].to_i * 3 }Ternary operators work like so:
someVar = x > 2 ? 1 : 2which is the same as:So
sum += i % 2 == 0 ? isbn13[i].to_i : isbn13[i].to_i * 3 }addsisbn13[i].to_itosumifiis even, and addsisbn13[i].to_i*3if it's not.The reason the function has a
?on the end is Ruby notation - any functions that return boolean should end with?Not sure if that's any use or not but maybe it is :P