Module: IOStruct::HexInspect

Includes:
InspectBase
Defined in:
lib/iostruct.rb

Constant Summary collapse

HEX_FMTS =
{ 1 => "%2x", 2 => "%4x", 4 => "%8x", 8 => "%16x" }.freeze

Constants included from InspectBase

InspectBase::INT_MASKS

Instance Method Summary collapse

Methods included from InspectBase

#to_table

Instance Method Details

#format_integer(value, size, fname) ⇒ Object

display as unsigned, because signed %x looks ugly: “..f” for -1



196
197
198
199
200
# File 'lib/iostruct.rb', line 196

def format_integer(value, size, fname)
  fmt  = HEX_FMTS[size]  || raise("Unsupported Integer size #{size} for field #{fname}")
  mask = INT_MASKS[size] || raise("Unsupported Integer size #{size} for field #{fname}")
  fmt % (value & mask)
end

#inspectObject



191
192
193
# File 'lib/iostruct.rb', line 191

def inspect
  to_s
end

#to_sObject



181
182
183
184
185
186
187
188
189
# File 'lib/iostruct.rb', line 181

def to_s
  "<#{self.class.name} " + to_h.map do |k, v|
    if v.is_a?(Integer) && v > 9
      "#{k}=0x%x" % v
    else
      "#{k}=#{v.inspect}"
    end
  end.join(' ') + ">"
end