Class: NewBase60

Inherits:
Object
  • Object
show all
Defined in:
lib/new_base_60.rb

Constant Summary collapse

VERSION =
'1.1.2'
VOCABULARY =
"0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz"

Instance Method Summary collapse

Constructor Details

#initialize(base_60) ⇒ NewBase60

Returns a new instance of NewBase60.



8
9
10
# File 'lib/new_base_60.rb', line 8

def initialize(base_60)
  @base_60 = base_60
end

Instance Method Details

#to_dateObject

Converts into a Date.



41
42
43
# File 'lib/new_base_60.rb', line 41

def to_date
  Time.at(to_i * 60 * 60 * 24).utc.to_date
end

#to_iObject

Converts into a base 10 integer.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/new_base_60.rb', line 17

def to_i
  num = 0

  @base_60.bytes do |char|
    case char
    when 48..57   then char -= 48
    when 65..72   then char -= 55
    when 73, 108  then char  = 1  # typo capital I, lowercase l to 1
    when 74..78   then char -= 56
    when 79       then char  = 0  # error correct typo capital O to 0
    when 80..90   then char -= 57
    when 95       then char  = 34
    when 97..107  then char -= 62
    when 109..122 then char -= 63
    else               char  = 0  # treat all other noise as 0
    end

    num = 60 * num + char
  end

  num
end

#to_sObject



12
13
14
# File 'lib/new_base_60.rb', line 12

def to_s
  @base_60
end