Method: CTypes::Union#pretty_print

Defined in:
lib/ctypes/union.rb

#pretty_print(q) ⇒ Object

:nodoc:



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/ctypes/union.rb', line 548

def pretty_print(q) # :nodoc:
  # before printing, apply any changes to the buffer
  apply_changes!

  active = to_h

  open = if (name = self.class.type_name || self.class.name)
    "union #{name} {"
  else
    "union {"
  end
  q.group(4, open, "}") do
    q.seplist(self.class.fields, -> { q.breakable("") }) do |name|
      q.text(".#{name} = ")
      unless active.has_key?(name)
        begin
          v = self.class.unpack_field(field: name, buf: @buf, endian: @endian)
          active.merge!(v)
        rescue Error => ex
          active[name] = "[unpack failed: %p]" % [ex]
        end
      end

      q.pp(active[name])
      q.text(", ")
    end
  end
end