Module: IOStruct::HexInspect

Defined in:
lib/iostruct.rb

Overview

InstanceMethods

Instance Method Summary collapse

Instance Method Details

#inspectObject



166
167
168
# File 'lib/iostruct.rb', line 166

def inspect
  to_s
end

#to_sObject



140
141
142
143
144
145
146
147
148
# File 'lib/iostruct.rb', line 140

def to_s
  "<#{self.class.to_s} " + 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

#to_tableObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/iostruct.rb', line 150

def to_table
  @fmtstr_tbl = "<#{self.class.to_s} " + self.class.const_get('FIELDS').map do |name, f|
    fmt =
      case 
      when f.type == Integer
        "%#{f.size*2}x"
      when f.type == Float
        "%8.3f"
      else
        "%s"
      end
    "#{name}=#{fmt}"
  end.join(' ') + ">"
  sprintf @fmtstr_tbl, *to_a.map{ |v| v.is_a?(String) ? v.inspect : (v||0) } # "||0" to avoid "`sprintf': can't convert nil into Integer" error
end