Last active
January 18, 2019 10:08
-
-
Save whysthatso/5916931 to your computer and use it in GitHub Desktop.
Ruby script for calculating the checksum of a 2..20 range integer following the 7-3-1 method.
Estonian Bank Union / Eesti Pangaliit - Reference number / Viitenumber
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
#!/usr/bin/env ruby | |
# | |
######################################################## | |
# | |
# What is this? | |
# | |
# script can calculate the checksum of a 2..20 range integer following the 7-3-1 method. | |
# | |
# Details | |
# 1. run on cli, script asks for number entry | |
# 2. calculates checksum | |
# 3. spits out original value and checksum | |
# | |
# Gossip | |
# This is the produce of a beginner ruby scripter, so any best-practice/bug-slashing/constructive | |
# comments are more than # appreciated. I hope this will help implement the reference number | |
# calculation more quickly for other starters than it took me. | |
# | |
# Credits | |
# def calc_ref_number is a translation of a php method from here: | |
# Estonian Bank Union / Eesti Pangaliit - Reference number / Viitenumber | |
# http://www.pangaliit.ee/en/settlements-and-standards/check-digit-calculator-of-domestic-account-number | |
# | |
############################################################ | |
#### Methods | |
# for use on cli, not sure if the instance method is good practice, but it works | |
def get_value | |
print '> ' | |
@value = gets.chomp | |
end | |
# Rescueing rather than regexing the check for an integer | |
# problems: 09, 08 trigger because they are interpreted as octal ( did not matter for use case) | |
def integer?(str) | |
!!Integer(str) rescue false | |
end | |
# check if this a valid entry (length, integer) | |
# i dare you, make this shorter | |
def check_value(str) | |
if integer?(str) | |
puts 'Value is integer' | |
#not sure if this is needed? | |
@value = str | |
case | |
when @value.length < 2 | |
puts "Value needs to be longer than 1" | |
get_value | |
check_value(@value) | |
when @value.length > 19 | |
puts "Value needs to be shortern than 19" | |
get_value | |
check_value(@value) | |
else | |
puts "Alright, here's your Reference number" | |
calc_ref_number(@value) | |
end | |
else | |
puts "Please enter a number" | |
get_value | |
check_value(@value) | |
end | |
end | |
#do the actual calculation | |
def calc_ref_number(int) | |
# split string of numbers and push it into array | |
weight = [1,3,7] | |
# associating sl and st with the integer value of the amount of digits | |
sl = st = int.length | |
total = 0 | |
# while lenght is higher than 0 and the last position on the string is higher than 0 | |
while sl > 0 && int[sl - 1] >= '0' | |
# calculate the last digit's product with the associated member of the array | |
total += subtotal = (int[st-1-sl].to_i * weight[ sl%3 ]) | |
sl = sl-1 | |
end | |
kontrollnr = (total.to_f / 10).ceil * 10 - total | |
puts int.to_s + kontrollnr.to_s | |
end | |
#### Program | |
puts 'Calculate reference number' | |
puts | |
puts 'Enter value between 2 and 20' | |
get_value | |
check_value(@value) |
Tested class for reference number generation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
maybe you could use regexp to validate the input: