Module: IOStruct::HexInspect
- Defined in:
- lib/iostruct.rb
Overview
InstanceMethods
Instance Method Summary collapse
Instance Method Details
#inspect ⇒ Object
179 180 181 |
# File 'lib/iostruct.rb', line 179 def inspect to_s end |
#to_s ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/iostruct.rb', line 153 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 |
#to_table ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/iostruct.rb', line 163 def to_table @fmtstr_tbl = "<#{self.class.name} " + 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 |