Module: IOStruct::InspectBase

Included in:
DecInspect, HexInspect
Defined in:
lib/iostruct.rb

Constant Summary collapse

INT_MASKS =
{ 1 => 0xff, 2 => 0xffff, 4 => 0xffffffff, 8 => 0xffffffffffffffff }.freeze

Instance Method Summary collapse

Instance Method Details

#to_tableObject

rubocop:disable Lint/DuplicateBranch



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/iostruct.rb', line 141

def to_table
  values = to_a
  "<#{self.class.name} " + self.class::FIELDS.map.with_index do |el, idx|
    v = values[idx]
    fname, f = el

    "#{fname}=" +
      case
      when f.nil? # unknown field type
        v.inspect
      when f.type == Integer
        v ||= 0 # avoid "`sprintf': can't convert nil into Integer" error
        format_integer(v, f.size, fname)
      when f.type == Float
        v ||= 0 # avoid "`sprintf': can't convert nil into Float" error
        "%8.3f" % v
      else
        v.inspect
      end
  end.join(' ') + ">"
end