Class: RbStruct::StructBase

Inherits:
Array
  • Object
show all
Defined in:
lib/rbstruct/rbstruct.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.bsizeObject (readonly)

Returns the value of attribute bsize.



17
18
19
# File 'lib/rbstruct/rbstruct.rb', line 17

def bsize
  @bsize
end

.fieldsObject (readonly)

Returns the value of attribute fields.



17
18
19
# File 'lib/rbstruct/rbstruct.rb', line 17

def fields
  @fields
end

.format_stringObject (readonly)

Returns the value of attribute format_string.



17
18
19
# File 'lib/rbstruct/rbstruct.rb', line 17

def format_string
  @format_string
end

.sizeObject (readonly)

Returns the value of attribute size.



17
18
19
# File 'lib/rbstruct/rbstruct.rb', line 17

def size
  @size
end

Class Method Details

.read(f, n = 1) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/rbstruct/rbstruct.rb', line 107

def self.read(f, n=1)
  format = self.format_string*n
  len = self.bsize*n
  array = f.read(len).unpack(format)
  return array.first if n == 1
  return Array.new(n) { self.new(array.slice!(0, self.size)) }
end

Instance Method Details

#inspect(d = 0) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rbstruct/rbstruct.rb', line 115

def inspect(d=0)
  self.class.to_s + " {\n" +
  self.class.fields.each_with_index.map {|f,n|
    field = self.get_field(n)
    "\t"*(d+1) + "#{f[:type]} #{f[:name]} = " +
      case field
      when StructBase
        "#{field.inspect(d+1)}"
      else
        field.inspect
      end

  }.join("\n") +
  "\n" + "\t"*d + "}"
end

#to_sObject



103
104
105
# File 'lib/rbstruct/rbstruct.rb', line 103

def to_s
  pack(self.class.format_string)
end