Class: Zold::Hexnum

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

Overview

A hex num

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num, length) ⇒ Hexnum

Returns a new instance of Hexnum.



29
30
31
32
# File 'lib/zold/hexnum.rb', line 29

def initialize(num, length)
  @num = num
  @length = length
end

Class Method Details

.parse(txt) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/zold/hexnum.rb', line 42

def self.parse(txt)
  n = Integer("0x#{txt}", 16)
  if txt.start_with?('f')
    max = Integer("0x#{'f' * txt.length}", 16)
    n = n - max - 1
  end
  Hexnum.new(n, txt.length)
end

Instance Method Details

#to_iObject



34
35
36
# File 'lib/zold/hexnum.rb', line 34

def to_i
  @num
end

#to_sObject



38
39
40
# File 'lib/zold/hexnum.rb', line 38

def to_s
  format("%0#{@length}x", @num).gsub(/^\.{2}/, 'ff')
end