Created: 2008-08-06 19:42
Updated: 2019-01-28 08:39
License: mit



Personnummer is a ruby class that handles Swedish personal identity number.


$ gem install personnummer

Example usage

>> p ='830428-5912')
#>>Personnummer:0x007fc971369100 @valid=true, @control_digit=2, @divider="-", @serial=591, @born=#>>PersonnummerDate:0x007fc971368318 @divider="-", @day=28, @month=4, @year=1983> #<, @region="Skaraborgs län", @female=false> #<

>> p.valid?
=> true

>> p ='830428')
=> ArgumentError: The supplied personnummer is invalid

This implementation also supports co-ordination numbers

Information about co-ordination numbers (in swedish)

These numbers are obtained by adding 60 to the day of the birth date

>> p ='830488-5912')
=> #<Personnummer:0x007fbafd1458d8 ...>
>> p.born
=> #<Date: 1983-04-28 ((2445453j,0s,0n),+0s,2299161j)>
>> p.co_ordination_number?
=> true


>> p1 =
=> #<Personnummer:0x007fb8fd816588 @valid=true, @control_digit=2, @divider="-", @serial=591, @born=#<Personnummer::Born:0x007fb8fd80fa80 @divider="-", @day=28, @month=4, @year=1983>, @region="Skaraborgs län", @female=false>

>> p1 == 8304285912
=> true
p1 == '8304285912'
=> true

Note on "reservnummer"

"Reservnummer" is used in some special cases. There are only recommendations how it should be formatted, no formalized standard.

You can read more about it on wikipedia.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more