Last active
August 29, 2015 14:23
-
-
Save umate/485e159d0f07e9b528bd to your computer and use it in GitHub Desktop.
ISBN Calculator
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
class ISBNCalculator | |
def initialize(barcode) | |
@barcode = barcode.to_s | |
end | |
def ISBN | |
"#{@barcode}#{check_digit}" | |
end | |
private | |
def check_digit | |
check_sum = 0 | |
@barcode.each_char.with_index(1) do |char, index| | |
digit = char.to_i | |
check_sum += index.odd? ? digit : digit*3 | |
end | |
(10 - check_sum % 10) % 10 | |
end | |
end | |
print "Please enter the barcode: " | |
barcode = gets.chomp | |
calculator = ISBNCalculator.new(barcode) | |
puts "The ISBN for this barcode is #{calculator.ISBN}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run this in the command line the next way:
$ ruby isbn_calculator.rb