class BitStruct::Field

Attributes

default[R]

Default value.

display_name[R]

Display name of field (used for printing).

format[R]

Format for printed value of field.

length[R]

Length of field in bits.

name[R]

Name of field (used for its accessors).

offset[R]

Offset of field in bits.

options[R]

Options, such as :default (varies for each field subclass). In general, options can be provided as strings or as symbols.

size[R]

Length of field in bits.

Public Class Methods

class_name() click to toggle source

Used in describe.

# File lib/bit-struct/field.rb, line 36
def self.class_name
  @class_name ||= name[/\w+$/]
end
default() click to toggle source

Subclasses can override this to define a default for all fields of this class, not just the one currently being added to a BitStruct class, a “default default” if you will. The global default, if default returns nil, is to fill the field with zero. Most field classes just let this default stand. The default can be overridden per-field when a BitStruct class is defined.

# File lib/bit-struct/field.rb, line 33
def self.default; nil; end
new(offset, length, name, opts = {}) click to toggle source

Options are display_name, default, and format (subclasses of Field may add other options).

# File lib/bit-struct/field.rb, line 67
def initialize(offset, length, name, opts = {})
  @offset, @length, @name, @options =
    offset, length, name, opts

  @display_name = opts[:display_name] || opts["display_name"]
  @default      = opts[:default] || opts["default"] || self.class.default
  @format       = opts[:format] || opts["format"]
end

Public Instance Methods

class_name() click to toggle source

Used in describe. Can be overridden per-subclass, as in NestedField.

# File lib/bit-struct/field.rb, line 41
def class_name
  self.class.class_name
end
describe(opts) { |"@%d" % byte_offset, class_name, name, len_str, display_name| ... } click to toggle source

Yield the description of this field, as an array of 5 strings: byte offset, type, name, size, and description. The opts hash may have:

:expand

if the value is true, expand complex fields

(Subclass implementations may yield more than once for complex fields.)

# File lib/bit-struct/field.rb, line 52
def describe opts
  bits = size
  if bits > 32 and bits % 8 == 0
    len_str = "%dB" % (bits/8)
  else
    len_str = "%db" % bits
  end

  byte_offset = offset / 8 + (opts[:byte_offset] || 0)

  yield ["@%d" % byte_offset, class_name, name, len_str, display_name]
end
inspect_in_object(obj, opts) click to toggle source

Inspect the value of this field in the specified obj.

# File lib/bit-struct/field.rb, line 77
def inspect_in_object(obj, opts)
  val = obj.send(name)
  str =
    begin
      val.inspect_with_options(opts)
    rescue NoMethodError
      val.inspect
    end
  (f=@format) ? (f % str) : str
end
inspectable?() click to toggle source

Normally, all fields show up in inspect, but some, such as padding, should not.

# File lib/bit-struct/field.rb, line 90
def inspectable?; true; end