Module: IOStruct::HexInspect

Defined in:
lib/iostruct.rb

Overview

InstanceMethods

Instance Method Summary collapse

Instance Method Details

#inspectObject



158
159
160
# File 'lib/iostruct.rb', line 158

def inspect
  to_s
end

#to_sObject



132
133
134
135
136
137
138
139
140
# File 'lib/iostruct.rb', line 132

def to_s
  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



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

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 }
end