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.



31
32
33
34
# File 'lib/zold/hexnum.rb', line 31

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

Class Method Details

.parse(txt) ⇒ Object



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

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



36
37
38
# File 'lib/zold/hexnum.rb', line 36

def to_i
  @num
end

#to_sObject



40
41
42
# File 'lib/zold/hexnum.rb', line 40

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