Class: FlexArray
- Includes:
- Enumerable, InArrayAlready
- Defined in:
- lib/flex_array.rb,
lib/flex_array/version.rb,
lib/flex_array/flex_array_new.rb,
lib/flex_array/flex_array_each.rb,
lib/flex_array/flex_array_index.rb,
lib/flex_array/flex_array_append.rb,
lib/flex_array/flex_array_forever.rb,
lib/flex_array/flex_array_process.rb,
lib/flex_array/flex_array_reshape.rb,
lib/flex_array/flex_array_validate.rb,
lib/flex_array/flex_array_transpose.rb
Overview
-
flex_array_transpose.rb - The flexible array class transpose and related methods.
Defined Under Namespace
Classes: ForEver
Constant Summary collapse
- VERSION =
The version string for flex array.
"0.3.2"- FOREVER =
Create a forever constant for use where infinite looping is needed.
ForEver.new
Instance Attribute Summary collapse
-
#array_data ⇒ Object
The underlying array data used by the flex array.
-
#array_specs ⇒ Object
The array specifications.
-
#transposed ⇒ Object
readonly
Is this flex array transposed?.
Class Method Summary collapse
-
.new_from(array_specs, other) ⇒ Object
Construct a FlexArray using other as a template or data source.
-
.new_from_array(other) ⇒ Object
Construct a FlexArray as a duplicate of a source array or flex array.
-
.new_from_selection(array_specs, other, selection) ⇒ Object
Construct a FlexArray using a all or a portion of portion of another FlexArray as a data source.
-
.version ⇒ Object
The version of this class.
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Append to the flex array.
-
#<=>(other) ⇒ Object
Make FlexArrays comparable.
-
#==(other) ⇒ Object
Are these FlexArrays equal?
Parameters * other - The object being tested for equality. -
#[](*indexes) ⇒ Object
Retrieve the selected data from the FlexArray.
-
#[]=(*indexes, value) ⇒ Object
Store the value data into the FlexArray.
-
#_each_raw(&block) ⇒ Object
A specialized each variant that passes the low level data, the index and the position to the block.
-
#_select_each_raw(indexes, &block) ⇒ Object
An enhanced specialized each variant that passes the low level data, the index and the position to the block.
-
#collect(&block) ⇒ Object
The flex array version of collect that returns a flex array.
-
#collect!(&block) ⇒ Object
The flex array version of collect!
Parameters * block - The required block to be executed for each selected array element. -
#compatible?(other) ⇒ Boolean
Is this array compatible with other?
Parameters * other - The object being tested for compatibility. -
#copy_data(other) ⇒ Object
Make a copy of the other’s data.
-
#cycle(count = FOREVER, &block) ⇒ Object
Retrieve data from the array endlessly repeating as needed.
-
#dimensions ⇒ Object
The number of dimensions in this array.
-
#dup ⇒ Object
Create a duplicate of this array.
-
#each(&block) ⇒ Object
Process the standard each operator.
-
#each_with_index(&block) ⇒ Object
Process the standard each_with_index operator.
-
#empty? ⇒ Boolean
Is this flex array empty?.
-
#find_index(value = nil, &block) ⇒ Object
The flex array version of find_index.
-
#find_indexes(value = nil, &block) ⇒ Object
The improved flex array version of find_index.
- #flatten_collect ⇒ Object
-
#initialize(array_specs, default = nil, &init_block) ⇒ FlexArray
constructor
Construct a flexible array object.
-
#length ⇒ Object
(also: #size)
The total number of elements in this array.
-
#limits ⇒ Object
Get the limits of the subscripts of the flex array.
-
#reshape(array_specs) ⇒ Object
Return a copy of this FlexArray, recast in a new shape, dropping or repeating data elements as required.
-
#reshape!(array_specs) ⇒ Object
Recast this FlexArray in a new shape, dropping or repeating data elements as required.
-
#select_collect(indexes, &block) ⇒ Object
The flex array version of collect that accepts an optional set of indexes to select the data being collected into a flex array.
-
#select_collect!(indexes, &block) ⇒ Object
The enhanced flex array version of collect! that accepts a set of indexes to select the data being collected.
-
#select_cycle(indexes, count = FOREVER, &block) ⇒ Object
Retrieve data from a subset of the flex array endlessly repeating as needed.
-
#select_each(indexes, &block) ⇒ Object
Process the enhanced select_each operator.
-
#select_each_with_index(indexes, &block) ⇒ Object
Process the enhanced select_each_with_index operator.
-
#select_find_index(indexes, value = nil, &block) ⇒ Object
The enhanced flex array version of find_index.
-
#select_find_indexes(indexes, value = nil, &block) ⇒ Object
The enhanced and improved flex array version of find_index.
-
#select_flatten_collect(indexes, &block) ⇒ Object
The flex array version of collect that accepts an optional set of indexes to select the data being collected into a standard array.
- #shallow_dup ⇒ Object
-
#to_a ⇒ Object
Convert the flex array to a simple array.
-
#to_flex_array ⇒ Object
Return this flex array as a flex array!
Returns * A flex array – self. -
#transpose(dim_a, dim_b) ⇒ Object
Return a reference to this array’s data with the specified dimensions transposed.
-
#transpose!(dim_a, dim_b) ⇒ Object
Transpose the specified dimensions.
-
#version ⇒ Object
The version of the class of this instance.
Constructor Details
#initialize(array_specs, default = nil, &init_block) ⇒ FlexArray
Construct a flexible array object.
Parameters
-
array_specs - The specification of the flex array subscript limits.
These can either be integers, in which case the limits are 0...n or
they can be ranges of integers, in which case the limits are those
of the range, or they can be an array of integers and/or ranges of
integers to represent arrays of more than one dimension.
-
default - The optional default value. Defaults to nil.
-
init_block - An optional initialization block. The return values from this
block are used to initialize the array. This overrides the default value.
Block Arguments
-
index - An array with one element per dimension with the fully qualified
index of the element being accessed. Note that the same array is passed
in each time the block is called, so, if this parameter is to stored
anywhere, it's best that it be cloned or duplicated first.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/flex_array/flex_array_new.rb', line 18 def initialize(array_specs, default=nil, &init_block) @array_specs = SpecArray.new(array_specs.in_array) @transposed = false #Allocate the data for the array. @array_data = Array.new(@array_specs.spec_count, default) #Set up the array with the optional init_block. if init_block process_all {|index, posn| @array_data[posn] = init_block.call(index)} end end |
Instance Attribute Details
#array_data ⇒ Object
The underlying array data used by the flex array.
45 46 47 |
# File 'lib/flex_array.rb', line 45 def array_data @array_data end |
#array_specs ⇒ Object
The array specifications. An array of spec components.
42 43 44 |
# File 'lib/flex_array.rb', line 42 def array_specs @array_specs end |
#transposed ⇒ Object (readonly)
Is this flex array transposed?
60 61 62 |
# File 'lib/flex_array.rb', line 60 def transposed @transposed end |
Class Method Details
.new_from(array_specs, other) ⇒ Object
Construct a FlexArray using other as a template or data source.
Parameters:
-
array_specs - The specification of the flex array subscript limits.
These can either be integers, in which case the limits are 0...n or
they can be ranges of integers, in which case the limits are those
of the range, or they can be an array of integers and/or ranges of
integers to represent arrays of more than one dimension.
-
other - The array or flex array that is the source of the data.
Note:
-
To make a copy of a flex array, use dup instead.
55 56 57 58 |
# File 'lib/flex_array/flex_array_new.rb', line 55 def self.new_from(array_specs, other) iterator = other.array_data.cycle FlexArray.new(array_specs) {iterator.next} end |
.new_from_array(other) ⇒ Object
Construct a FlexArray as a duplicate of a source array or flex array.
Parameters
-
other - the array or flex array that is the source.
Returns
-
A flex array.
Note
-
The data array of the new flex array is a reference to the source array.
88 89 90 91 92 93 |
# File 'lib/flex_array/flex_array_new.rb', line 88 def self.new_from_array(other) result = FlexArray.new(0) result.array_specs = other.array_specs.dup result.array_data = other.array_data result end |
.new_from_selection(array_specs, other, selection) ⇒ Object
Construct a FlexArray using a all or a portion of portion of another FlexArray as a data source.
Parameters
-
array_specs - The specification of the flex array subscript limits.
These can either be integers, in which case the limits are 0...n or
they can be ranges of integers, in which case the limits are those
of the range, or they can be an array of integers and/or ranges of
integers to represent arrays of more than one dimension.
-
other - The flex array source of the data used to build this flex
array. If no limits or other_indexes are specified, the limits of the
other array are used instead.
-
selection - The indexes of the required sub-set of data to use
in setting up the new array.
Notes:
-
If the entire source array is to be used, use new_from instead.
-
To make a copy of a flex array, use dup instead.
76 77 78 79 |
# File 'lib/flex_array/flex_array_new.rb', line 76 def self.new_from_selection(array_specs, other, selection) iterator = other.select_cycle(selection) FlexArray.new(array_specs) {iterator.next} end |
.version ⇒ Object
The version of this class.
Returns
-
A version string; <major>.<minor>.<step>
30 31 32 |
# File 'lib/flex_array.rb', line 30 def self.version FlexArray::VERSION end |
Instance Method Details
#<<(data) ⇒ Object
Append to the flex array.
Parameters
-
data - the data being appended to this array.
Returns
-
The array spec of the array being appended.
Exceptions
-
ArgumentError if the data may not be appended.
10 11 12 13 14 15 16 |
# File 'lib/flex_array/flex_array_append.rb', line 10 def << (data) fail "Cannot append to a transposed array." if @transposed specs = get_append_specs(data = data.in_array) @array_data += data.array_data @array_specs.enlarge(specs[0].span) self end |
#<=>(other) ⇒ Object
Make FlexArrays comparable.
Parameters
-
other - The object being tested for compariositality.
Returns
-
1 if self > other
-
0 if self = other
-
-1 if self < other
90 91 92 |
# File 'lib/flex_array.rb', line 90 def <=>(other) @array_data <=> other.array_data end |
#==(other) ⇒ Object
Are these FlexArrays equal?
Parameters
-
other - The object being tested for equality.
Returns
-
true if the flex arrays are equal shape and data.
79 80 81 |
# File 'lib/flex_array.rb', line 79 def ==(other) self.compatible?(other) && @array_data == other.array_data end |
#[](*indexes) ⇒ Object
Retrieve the selected data from the FlexArray.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. These entries may be an integer that is in the bounds of
the range limit of that dimension, or it can be a range where the start
and end of the range are in the bounds of the range limit of that
dimension, or it can be the symbol :all for all of the possible values
of that dimension.
Returns
-
The data selected by the index or an array of data if the index selects
more than one cell.
Note
-
If the indexes parameter equals a single :all this maps to all
elements of the array, regardless of its dimensionality.
17 18 19 20 21 22 |
# File 'lib/flex_array/flex_array_index.rb', line 17 def [](*indexes) validate_index_count(indexes) result = [] process_indexes(indexes) {|_index, posn| result << @array_data[posn]} result.length == 1 ? result[0] : result end |
#[]=(*indexes, value) ⇒ Object
Store the value data into the FlexArray.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. These entries may be an integer that is in the bounds of
the range limit of that dimension, or it can be a range where the start
and end of the range are in the bounds of the range limit of that
dimension, or it can be the symbol :all for all of the possible values
of that dimension.
-
value - A value to be stored in the selected array entries.
Returns
-
The value stored in the array.
Note
-
If the indexes parameter equals a single :all this maps to all
elements of the array, regardless of its dimensionality.
38 39 40 41 42 43 |
# File 'lib/flex_array/flex_array_index.rb', line 38 def []=(*indexes, value) validate_index_count(indexes) source = value.in_array.cycle process_indexes(indexes) {|_index, posn| @array_data[posn] = source.next} value end |
#_each_raw(&block) ⇒ Object
A specialized each variant that passes the low level data, the index and the position to the block.
Parameters
-
block - The optional block to be executed for each selected array element.
The return value is the last value returned by the block. If the block
is not present, then an enumerator object is returned.
Returns
-
If the block is not present, then an enumerator object is returned.
Otherwise the flex array is returned.
Block Arguments
-
index - An array with the full index of the selected value.
-
posn - The position of the data in the low level data store.
168 169 170 171 172 173 174 175 |
# File 'lib/flex_array/flex_array_each.rb', line 168 def _each_raw(&block) if block_given? process_all {|index, posn| block.call(index, posn)} self else self.to_enum(:_each_raw) end end |
#_select_each_raw(indexes, &block) ⇒ Object
An enhanced specialized each variant that passes the low level data, the index and the position to the block.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. See [] for more details. Note that since indexes is NOT
a splat parameter, it must be passed as an array explicitly.
-
block - The optional block to be executed for each selected array element.
The return value is the last value returned by the block. If the block
is not present, then an enumerator object is returned.
Returns
-
If the block is not present, then an enumerator object is returned.
Otherwise the flex array is returned.
Block Arguments
-
index - An array with the full index of the selected value.
-
posn - The position of the data in the low level data store.
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/flex_array/flex_array_each.rb', line 192 def _select_each_raw(indexes, &block) validate_index_count(indexes) if block_given? process_indexes(indexes) {|index, posn| block.call(index, posn)} self else self.to_enum(:_select_each_raw, indexes) end end |
#collect(&block) ⇒ Object
The flex array version of collect that returns a flex array.
Parameters
-
block - The optional block to be executed for each selected array element.
Returns
-
An array of the computed objects retuned by the block.
Block Arguments
-
value - Each value selected by the iteration.
212 213 214 215 |
# File 'lib/flex_array/flex_array_each.rb', line 212 def collect(&block) result = self.dup result.collect!(&block) end |
#collect!(&block) ⇒ Object
The flex array version of collect!
Parameters
-
block - The required block to be executed for each selected array element.
The return value is the last value returned by the block. If the block
is not present, an enumerator object is returned.
Returns
-
self
Block Arguments
-
value - Each value selected by the iteration.
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/flex_array/flex_array_each.rb', line 267 def collect!(&block) fail ArgumentError, "A block is required." unless block_given? if @transposed process_all {|_index, posn| @array_data[posn] = block.call(@array_data[posn])} else @array_data = @array_data.collect(&block) end self end |
#compatible?(other) ⇒ Boolean
Is this array compatible with other?
Parameters
-
other - The object being tested for compatibility.
Returns
-
true if the arrays are compatible.
8 9 10 11 12 |
# File 'lib/flex_array/flex_array_validate.rb', line 8 def compatible?(other) @array_specs == other.array_specs rescue Exception false end |
#copy_data(other) ⇒ Object
Make a copy of the other’s data.
Parameters
-
other - The flex array whose data is to be copied.
21 22 23 24 |
# File 'lib/flex_array/flex_array_append.rb', line 21 def copy_data(other) fail ArgumentError, "Incompatible data copy." unless compatible?(other) @array_data = other.array_data.dup end |
#cycle(count = FOREVER, &block) ⇒ Object
Retrieve data from the array endlessly repeating as needed.
Parameters
-
count - The number of times to cycle through the flex array. Defaults to
cycling forever.
-
block - The optional block to be executed for each selected array element.
The return value is the last value returned by the block. If the block
is not present, then an enumerator object is returned.
Returns
-
nil or an enumerator if no block is provided.
Block Arguments
-
value - Each value selected by the iteration.
Endemic Code Smells
-
:reek:NestedIterators
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/flex_array/flex_array_each.rb', line 19 def cycle(count = FOREVER, &block) if block_given? if @transposed && length > 0 count.times do process_all do |_index, posn| block.call(@array_data[posn]) end end nil else @array_data.cycle(count.to_i, &block) end else self.to_enum(:cycle, count) end end |
#dimensions ⇒ Object
The number of dimensions in this array.
55 56 57 |
# File 'lib/flex_array.rb', line 55 def dimensions @array_specs.spec_dimensions end |
#dup ⇒ Object
Create a duplicate of this array.
Warning
-
The rdoc tool messes this up. shallow_dup is NOT an alias for this
dup, but rather the original system implementation of dup. This dup,
overrides the default dup and calls shallow_dup as part of its processing.
38 39 40 41 42 43 |
# File 'lib/flex_array/flex_array_new.rb', line 38 def dup other = self.shallow_dup other.array_specs = @array_specs.dup other.array_data = @array_data.dup other end |
#each(&block) ⇒ Object
Process the standard each operator.
Parameters
-
block - The optional block to be executed for each selected array element.
Returns
-
If the block is not present, then an enumerator object is returned.
Otherwise the flex array is returned.
Block Arguments
-
value - Each value selected by the iteration.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/flex_array/flex_array_each.rb', line 79 def each(&block) if block_given? if @transposed process_all {|_index, posn| block.call(@array_data[posn])} else @array_data.each(&block) end self else self.to_enum(:each) end end |
#each_with_index(&block) ⇒ Object
Process the standard each_with_index operator.
Parameters
-
block - The optional block to be executed for each selected array element.
Returns
-
If the block is not present, then an enumerator object is returned.
Otherwise the flex array is returned.
Block Arguments
-
value - Each value selected by the iteration.
-
index - An array with the full index of the selected value.
124 125 126 127 128 129 130 131 |
# File 'lib/flex_array/flex_array_each.rb', line 124 def each_with_index(&block) if block_given? process_all {|index, posn| block.call(@array_data[posn], index)} self else self.to_enum(:each_with_index) end end |
#empty? ⇒ Boolean
Is this flex array empty?
95 96 97 |
# File 'lib/flex_array.rb', line 95 def empty? length == 0 end |
#find_index(value = nil, &block) ⇒ Object
The flex array version of find_index. This returns the coordinates of the first object that matches the search object or is flagged true by the search block.
Parameters
-
object - The optional value to search for.
-
block - The optional block to be executed for each selected array element.
If the block or object are not present, an enumerator object is returned.
Returns
-
The index of the first place that matched or nil if none matched.
Block Arguments
-
value - Each value selected by the iteration.
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/flex_array/flex_array_each.rb', line 314 def find_index(value = nil, &block) blk = get_find_block(value, &block) if blk process_all do |index, posn| if blk.call(@array_data[posn]) return index end end nil else self.to_enum(:find_index) end end |
#find_indexes(value = nil, &block) ⇒ Object
The improved flex array version of find_index. This returns the coordinates of objects that match the search object or are flagged true by the search block.
Parameters
-
object - The optional value to search for.
-
block - The optional block to be executed for each selected array element.
If the block or object are not present, an enumerator object is returned.
Returns
-
An array of the indexes of the places that matched.
Block Arguments
-
value - Each value selected by the iteration.
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/flex_array/flex_array_each.rb', line 372 def find_indexes(value = nil, &block) blk, result = get_find_block(value, &block), [] if blk process_all do |index, posn| if blk.call(@array_data[posn]) result << index.dup end end result else self.to_enum(:find_indexes) end end |
#flatten_collect ⇒ Object
203 |
# File 'lib/flex_array/flex_array_each.rb', line 203 alias_method :flatten_collect, :collect |
#length ⇒ Object Also known as: size
The total number of elements in this array.
48 49 50 |
# File 'lib/flex_array.rb', line 48 def length @array_specs.spec_count end |
#limits ⇒ Object
Get the limits of the subscripts of the flex array.
63 64 65 |
# File 'lib/flex_array.rb', line 63 def limits @array_specs.collect {|spec| spec.range } end |
#reshape(array_specs) ⇒ Object
Return a copy of this FlexArray, recast in a new shape, dropping or repeating data elements as required.
Parameters
-
array_specs - The specification of the flex array subscript limits.
These can either be integers, in which case the limits are 0...n or
they can be ranges of integers, in which case the limits are those
of the range, or they can be an array of integers and/or ranges of
integers to represent arrays of more than one dimension.
Returns
-
The reshaped flexible array.
13 14 15 16 |
# File 'lib/flex_array/flex_array_reshape.rb', line 13 def reshape(array_specs) iterator = @array_data.cycle FlexArray.new(array_specs) {iterator.next} end |
#reshape!(array_specs) ⇒ Object
Recast this FlexArray in a new shape, dropping or repeating data elements as required.
Parameters
-
array_specs - The specification of the flex array subscript limits.
These can either be integers, in which case the limits are 0...n or
they can be ranges of integers, in which case the limits are those
of the range, or they can be an array of integers and/or ranges of
integers to represent arrays of more than one dimension.
Returns
-
The reshaped, flexible array.
28 29 30 31 32 |
# File 'lib/flex_array/flex_array_reshape.rb', line 28 def reshape!(array_specs) temp = self.reshape(array_specs) @array_specs, @array_data = temp.array_specs, temp.array_data self end |
#select_collect(indexes, &block) ⇒ Object
The flex array version of collect that accepts an optional set of indexes to select the data being collected into a flex array.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. See [] for more details. Note that since indexes is NOT
a splat parameter, it must be passed as an array explicitly.
-
block - The optional block to be executed for each selected array element.
Returns
-
An array of the computed objects retuned by the block.
If the block is not present, an enumerator object is returned.
Block Arguments
-
value - Each value selected by the iteration.
229 230 231 232 |
# File 'lib/flex_array/flex_array_each.rb', line 229 def select_collect(indexes, &block) result = self.dup result.select_collect!(indexes, &block) end |
#select_collect!(indexes, &block) ⇒ Object
The enhanced flex array version of collect! that accepts a set of indexes to select the data being collected.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. See [] for more details. Note that since indexes is NOT
a splat parameter, it must be passed as an array explicitly.
-
block - The required block to be executed for each selected array element.
The return value is the last value returned by the block. If the block
is not present, an enumerator object is returned.
Returns
-
self
Block Arguments
-
value - Each value selected by the iteration.
293 294 295 296 297 298 299 300 301 |
# File 'lib/flex_array/flex_array_each.rb', line 293 def select_collect!(indexes, &block) fail ArgumentError, "A block is required." unless block_given? validate_index_count(indexes) process_indexes(indexes) {|_index, posn| @array_data[posn] = block.call(@array_data[posn])} self end |
#select_cycle(indexes, count = FOREVER, &block) ⇒ Object
Retrieve data from a subset of the flex array endlessly repeating as needed.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. See [] for more details. Note that since indexes is NOT
a splat parameter, it must be passed as an array explicitly.
-
count - The number of times to cycle through the flex array. Defaults to
cycling forever.
-
block - The optional block to be executed for each selected array element.
The return value is the last value returned by the block. If the block
is not present, then an enumerator object is returned.
Returns
-
nil or an enumerator if no block is provided.
Block Arguments
-
value - Each value selected by the iteration.
Endemic Code Smells
-
:reek:NestedIterators
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/flex_array/flex_array_each.rb', line 53 def select_cycle(indexes, count = FOREVER, &block) validate_index_count(indexes) if block_given? unless empty? count.times do process_indexes(indexes) do |_index, posn| block.call(@array_data[posn]) end end end nil else self.to_enum(:select_cycle, indexes, count) end end |
#select_each(indexes, &block) ⇒ Object
Process the enhanced select_each operator.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. See [] for more details. Note that since indexes is NOT
a splat parameter, it must be passed as an array explicitly
-
block - The optional block to be executed for each selected array element.
Returns
-
If the block is not present, then an enumerator object is returned.
Otherwise the flex array is returned.
Block Arguments
-
value - Each value selected by the iteration.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/flex_array/flex_array_each.rb', line 104 def select_each(indexes, &block) validate_index_count(indexes) if block_given? process_indexes(indexes) {|_index, posn| block.call(@array_data[posn])} self else self.to_enum(:select_each, indexes) end end |
#select_each_with_index(indexes, &block) ⇒ Object
Process the enhanced select_each_with_index operator.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. See [] for more details. Note that since indexes is NOT
a splat parameter, it must be passed as an array explicitly.
-
block - The optional block to be executed for each selected array element.
Returns
-
If the block is not present, then an enumerator object is returned.
Otherwise the flex array is returned.
Block Arguments
-
value - Each value selected by the iteration.
-
index - An array with the full index of the selected value.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/flex_array/flex_array_each.rb', line 145 def select_each_with_index(indexes, &block) validate_index_count(indexes) if block_given? process_indexes(indexes) {|index, posn| block.call(@array_data[posn], index)} self else self.to_enum(:select_each_with_index, indexes) end end |
#select_find_index(indexes, value = nil, &block) ⇒ Object
The enhanced flex array version of find_index. This returns the coordinates of the first object that matches the search object or is flagged true by the search block.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. See [] for more details. Note that since indexes is NOT
a splat parameter, it must be passed as an array explicitly.
-
object - The optional value to search for.
-
block - The optional block to be executed for each selected array element.
If the block or object are not present, an enumerator object is returned.
Returns
-
The index of the first place that matched or nil if none matched.
Block Arguments
-
value - Each value selected by the iteration.
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/flex_array/flex_array_each.rb', line 344 def select_find_index(indexes, value = nil, &block) validate_index_count(indexes) blk = get_find_block(value, &block) if blk process_indexes(indexes) do |index, posn| if blk.call(@array_data[posn]) return index end end nil else self.to_enum(:select_find_index, indexes) end end |
#select_find_indexes(indexes, value = nil, &block) ⇒ Object
The enhanced and improved flex array version of find_index. This returns the coordinates of objects that match the search object or are flagged true by the search block.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. See [] for more details. Note that since indexes is NOT
a splat parameter, it must be passed as an array explicitly.
-
object - The optional value to search for.
-
block - The optional block to be executed for each selected array element.
If the block or object are not present, an enumerator object is returned.
Returns
-
An array of the indexes of the places that matched.
Block Arguments
-
value - Each value selected by the iteration.
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/flex_array/flex_array_each.rb', line 402 def select_find_indexes(indexes, value = nil, &block) validate_index_count(indexes) blk, result = get_find_block(value, &block), [] if blk process_indexes(indexes) do |index, posn| if blk.call(@array_data[posn]) result << index.dup end end result else self.to_enum(:select_find_index, indexes) end end |
#select_flatten_collect(indexes, &block) ⇒ Object
The flex array version of collect that accepts an optional set of indexes to select the data being collected into a standard array.
Parameters
-
indexes - An array with as many entries as the flexible array has
dimensions. See [] for more details. Note that since indexes is NOT
a splat parameter, it must be passed as an array explicitly.
-
block - The optional block to be executed for each selected array element.
Returns
-
An array of the computed objects retuned by the block.
If the block is not present, an enumerator object is returned.
Block Arguments
-
value - Each value selected by the iteration.
246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/flex_array/flex_array_each.rb', line 246 def select_flatten_collect(indexes, &block) validate_index_count(indexes) if block_given? result = [] process_indexes(indexes) {|_index, posn| result << block.call(@array_data[posn])} result else self.to_enum(:select_collect, indexes) end end |
#shallow_dup ⇒ Object
31 |
# File 'lib/flex_array/flex_array_new.rb', line 31 alias_method :shallow_dup, :dup |
#to_a ⇒ Object
Convert the flex array to a simple array. Contained arrays are not affected.
35 36 37 |
# File 'lib/flex_array/flex_array_reshape.rb', line 35 def to_a @array_data.dup end |
#to_flex_array ⇒ Object
Return this flex array as a flex array!
Returns
-
A flex array – self
70 71 72 |
# File 'lib/flex_array.rb', line 70 def to_flex_array self end |
#transpose(dim_a, dim_b) ⇒ Object
Return a reference to this array’s data with the specified dimensions transposed. This may change the “shape” of the array if the transposed dimensions were of different limits.
Returns
-
The flex array transposed.
Note
-
Transposing an array disables the speed-up for processing the array with
an index of [:all].
26 27 28 |
# File 'lib/flex_array/flex_array_transpose.rb', line 26 def transpose(dim_a, dim_b) FlexArray.new_from_array(self).transpose!(dim_a, dim_b) end |
#transpose!(dim_a, dim_b) ⇒ Object
Transpose the specified dimensions. This may change the “shape” of the array if the transposed dimensions were of different limits.
Returns
-
The flex array transposed.
Note
-
Transposing an array disables the speed-up for processing the array with
an index of [:all].
10 11 12 13 14 15 16 |
# File 'lib/flex_array/flex_array_transpose.rb', line 10 def transpose!(dim_a, dim_b) validate_dimension(dim_a) validate_dimension(dim_b) @array_specs[dim_a], @array_specs[dim_b] = @array_specs[dim_b], @array_specs[dim_a] @transposed = @array_specs.transposed? self end |
#version ⇒ Object
The version of the class of this instance.
Returns
-
A version string; <major>.<minor>.<step>
37 38 39 |
# File 'lib/flex_array.rb', line 37 def version FlexArray::VERSION end |