Skip to content

Instantly share code, notes, and snippets.

@umate
Last active August 29, 2015 14:23
Show Gist options
  • Save umate/485e159d0f07e9b528bd to your computer and use it in GitHub Desktop.
Save umate/485e159d0f07e9b528bd to your computer and use it in GitHub Desktop.
ISBN Calculator
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}"
@umate
Copy link
Author

umate commented Jun 16, 2015

Run this in the command line the next way:
$ ruby isbn_calculator.rb

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