Class: XDR::Array

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

Instance Method Summary collapse

Methods included from Concerns::ConvertsToXDR

#from_xdr, #to_xdr

Constructor Details

#initialize(child_type, length) ⇒ Array

Returns a new instance of Array.



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

def initialize(child_type, length)
  @child_type = child_type
  @length = length
end

Instance Method Details

#read(io) ⇒ Object



21
22
23
# File 'lib/xdr/array.rb', line 21

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

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/xdr/array.rb', line 25

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

#write(val, io) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
# File 'lib/xdr/array.rb', line 12

def write(val, io)
  raise XDR::WriteError, "val is not array" unless val.is_a?(Array)
  raise XDR::WriteError, "array must be #{@length} long, was #{val.length}" if val.length != @length

  @length.times do |i|
    @child_type.write val[i], io
  end
end