Class: XDR::VarArray

Inherits:
Object
  • Object
show all
Includes:
Concerns::ArrayConverter, Concerns::ConvertsToXDR
Defined in:
lib/xdr/var_array.rb

Instance Method Summary collapse

Methods included from Concerns::ConvertsToXDR

#from_xdr, #to_xdr

Constructor Details

#initialize(child_type, length = XDR::MAX_SIZE) ⇒ VarArray

Returns a new instance of VarArray.



7
8
9
10
# File 'lib/xdr/var_array.rb', line 7

def initialize(child_type, length = XDR::MAX_SIZE)
  @child_type = child_type
  @length = length
end

Instance Method Details

#read(io) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/xdr/var_array.rb', line 25

def read(io)
  length = XDR::Int.read(io)

  if length > @length
    raise XDR::ReadError, "VarArray length #{length} is greater than max #{@length}"
  end

  length.times.map { @child_type.read(io) }
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/xdr/var_array.rb', line 35

def valid?(val)
  super(val) && val.length <= @length
end

#write(val, io) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/xdr/var_array.rb', line 12

def write(val, io)
  length = val.length

  if length > @length
    raise XDR::WriteError, "Value length #{length} exceeds max #{@length}"
  end

  XDR::Int.write(length, io)
  val.each do |member|
    @child_type.write member, io
  end
end