An Array is a list of data objects of the same type.
require 'bindata' data = "\x03\x04\x05\x06\x07\x08\x09" obj = BinData::Array.new(:type => :int8, :initial_length => 6) obj.read(data) #=> [3, 4, 5, 6, 7, 8] obj = BinData::Array.new(:type => :int8, :read_until => lambda { index == 1 }) obj.read(data) #=> [3, 4] obj = BinData::Array.new(:type => :int8, :read_until => lambda { element >= 6 }) obj.read(data) #=> [3, 4, 5, 6] obj = BinData::Array.new(:type => :int8, :read_until => lambda { array[index] + array[index - 1] == 13 }) obj.read(data) #=> [3, 4, 5, 6, 7] obj = BinData::Array.new(:type => :int8, :read_until => :eof) obj.read(data) #=> [3, 4, 5, 6, 7, 8, 9]
Parameters may be provided at initialisation to control the behaviour of an object. These params are:
:type |
The symbol representing the data type of the array elements. If the type is to have params passed to it, then it should be provided as [type_symbol, hash_params]. |
:initial_length |
The initial length of the array. |
:read_until |
While reading, elements are read until this condition is true. This is typically used to read an array until a sentinel value is found. The variables index, element and array are made available to any lambda assigned to this parameter. If the value of this parameter is the symbol :eof, then the array will read as much data from the stream as possible. |
Each data object in an array has the variable index made available to any lambda evaluated as a parameter of that data object.
Returns the element at index.
# File lib/bindata/array.rb, line 146 def [](arg1, arg2 = nil) if arg1.respond_to?(:to_int) and arg2.nil? slice_index(arg1.to_int) elsif arg1.respond_to?(:to_int) and arg2.respond_to?(:to_int) slice_start_length(arg1.to_int, arg2.to_int) elsif arg1.is_a?(Range) and arg2.nil? slice_range(arg1) else raise TypeError, "can't convert #{arg1} into Integer" unless arg1.respond_to?(:to_int) raise TypeError, "can't convert #{arg2} into Integer" unless arg2.respond_to?(:to_int) end end
Sets the element at index.
# File lib/bindata/array.rb, line 181 def []=(index, value) extend_array(index) elements[index].assign(value) end
# File lib/bindata/array.rb, line 101 def assign(array) raise ArgumentError, "can't set a nil value for #{debug_name}" if array.nil? @element_list = to_storage_formats(array.to_ary) end
Returns the element at index. Unlike slice, if index is out of range the array will not be automatically extended.
# File lib/bindata/array.rb, line 176 def at(index) elements[index] end
# File lib/bindata/array.rb, line 97 def clear? @element_list.nil? or elements.all? { |el| el.clear? } end
# File lib/bindata/array.rb, line 134 def concat(array) insert(-1, *array.to_ary) self end
# File lib/bindata/array.rb, line 226 def each elements.each { |el| yield el } end
# File lib/bindata/array.rb, line 111 def find_index(obj) elements.index(obj) end
Returns the first index of obj in self.
Uses equal? for the comparator.
# File lib/bindata/array.rb, line 119 def find_index_of(obj) elements.index { |el| el.equal?(obj) } end
Returns the first element, or the first n elements, of the array. If the array is empty, the first form returns nil, and the second form returns an empty array.
# File lib/bindata/array.rb, line 189 def first(n = nil) if n.nil? and empty? # explicitly return nil as arrays grow automatically nil elsif n.nil? self[0] else self[0, n] end end
# File lib/bindata/array.rb, line 93 def initialize_instance @element_list = nil end
# File lib/bindata/array.rb, line 139 def insert(index, *objs) extend_array(index - 1) elements.insert(index, *to_storage_formats(objs)) self end
Returns the last element, or the last n elements, of the array. If the array is empty, the first form returns nil, and the second form returns an empty array.
# File lib/bindata/array.rb, line 203 def last(n = nil) if n.nil? self[-1] else n = length if n > length self[-n, n] end end
# File lib/bindata/array.rb, line 212 def length elements.length end
# File lib/bindata/array.rb, line 123 def push(*args) insert(-1, *args) self end
# File lib/bindata/array.rb, line 107 def snapshot elements.collect { |el| el.snapshot } end
Generated with the Darkfish Rdoc Generator 2.