Class: Numeric

Inherits:
Object show all
Defined in:
lib/rdoba/bcd.rb,
lib/rdoba/roman.rb,
lib/rdoba/numeric.rb

Constant Summary collapse

Roman =
{ 1 => 'I', 4 => 'IV', 5 => 'V', 9 => 'IX', 10 => 'X', 40 => 'XL', 50 => 'L',
90 => 'XC', 100 => 'C', 400 => 'CD', 500 => 'D', 900 => 'CM', 1000 => 'M' }
Romani =
Roman.keys.sort

Instance Method Summary collapse

Instance Method Details

#to_bcdObject



58
59
60
# File 'lib/rdoba/bcd.rb', line 58

def to_bcd
# NOTE: return as plain LSB string
BCD.parse value; end

#to_p(*opts) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rdoba/numeric.rb', line 53

def to_p(*opts)
  v = parse_opts(opts)

  value = self
  minus = if value < 0
      value = -value
      true
    end
  res = ''
  while value != 0
    value, rem = value.divmod(256)
    res += rem.chr
  end

  pad_char = if minus
      negres += ''
      over = 1
      res.each_byte do |byte|
        negbyte = 255 - byte + over
        negres += if negbyte > 255
            over = 1
            0
          else
            over = 0
            negbyte
          end
      end
      res = negres
      "\xFF"
    else
      "\0"
    end

  res += pad_char * (v[:padding].to_i - res.size) if res.size < v[:padding].to_i

  plain = (v[:be] ? res.reverse(String::ByteByByte) : res).to_p
  plain
end

#to_romObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rdoba/roman.rb', line 8

def to_rom
  res = ''
  num = self
  i = Romani.size - 1

  while num > 0
    if num >= Romani[i]
      res << Roman[Romani[i]]
      num -= Romani[i]
    else
      i -= 1
    end
  end
  res
end