Class: Hotfile::Integer

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

Overview

Parses a string integer (possibly overpunched) into a real Integer

Instance Method Summary collapse

Constructor Details

#initialize(int_string) ⇒ Integer

Returns a new instance of Integer.



6
7
8
# File 'lib/hotfile/integer.rb', line 6

def initialize(int_string)
  @raw_value = +int_string
end

Instance Method Details

#negative?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/hotfile/integer.rb', line 24

def negative?
  '}JKLMNOPQR'.include? @raw_value[-1]
end

#to_iObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hotfile/integer.rb', line 10

def to_i
  temp_string = @raw_value.clone
  overpunch_char = temp_string[-1]
  last_digit = case overpunch_char
                 when '{', '}' then 0
                 when /[0-9]/ then overpunch_char.to_i
                 when /[A-I]/ then overpunch_char.unpack1('c') - 64
                 when /[J-R]/ then overpunch_char.unpack1('c') - 73
               end
  temp_string[-1] = last_digit.to_s
  temp_string = "-#{temp_string}" if negative?
  temp_string.to_i
end