Here is a method for generating a Finnish reference number in Ruby.

It’s based on reference number specification published by Federation of Finnish Financial Services.

def finnish_reference_number(number, format = true)
  return false if number.length < 3
  return false if number.length > 19

  sum = 0
  digits = number.split(//)
  ([7, 3, 1] * 7).each do |f|
    break if !(n = digits.pop)
    sum += (f * n.to_i)
  end

  checksum = (10 - (sum % 10))  % 10
   reference_number = number.to_s + checksum.to_s

  if format
    reference_number = reference_number.sub(/^0+/, "").reverse
    reference_number.scan(/.....|.*/).join(" ").reverse.strip
  else
    reference_number
  end
end

finnish_reference_number("12")             #=> false
finnish_reference_number("123")            #=> "1232" 
finnish_reference_number("1234")           #=> "12344" 
finnish_reference_number("12345")          #=> "1 23453" 
finnish_reference_number("123456")         #=> "12 34561" 
finnish_reference_number("1234567")        #=> "123 45672" 
finnish_reference_number("12345678")       #=> "1234 56780" 
finnish_reference_number("123456789")      #=> "12345 67897" 
finnish_reference_number("1234567890")     #=> "1 23456 78907" 
finnish_reference_number("0001234567890")  #=> "1 23456 78907" 
finnish_reference_number("123456", false)  #=> "1234561"