Class: Canoser::ArrayT

Inherits:
Object
  • Object
show all
Defined in:
lib/canoser/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = Uint8, fixed_len = nil) ⇒ ArrayT

Returns a new instance of ArrayT.



106
107
108
109
# File 'lib/canoser/field.rb', line 106

def initialize(type=Uint8, fixed_len=nil)
  @type = type
  @fixed_len = fixed_len
end

Instance Attribute Details

#fixed_lenObject

Returns the value of attribute fixed_len.



104
105
106
# File 'lib/canoser/field.rb', line 104

def fixed_len
  @fixed_len
end

#typeObject

Returns the value of attribute type.



104
105
106
# File 'lib/canoser/field.rb', line 104

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



130
131
132
# File 'lib/canoser/field.rb', line 130

def ==(other)
  return self.type == other.type && self.fixed_len == other.fixed_len
end

#decode(cursor) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/canoser/field.rb', line 118

def decode(cursor)
  arr = []
  len = Uint32.decode(cursor)
  if @fixed_len && len != @fixed_len
    raise ParseError.new("fix-len:#{@fixed_len} != #{len}")
  end
  len.times do
    arr << @type.decode(cursor)
  end
  arr
end

#encode(arr) ⇒ Object



111
112
113
114
115
116
# File 'lib/canoser/field.rb', line 111

def encode(arr)
  output = ""
  output << Uint32.encode(arr.size)
  arr.each{|x| output << @type.encode(x)}
  output
end