Module: Snow::ArraySupport
- Includes:
- Enumerable
- Included in:
- Mat3, Mat3Array, Mat4, Mat4Array, Quat, QuatArray, Vec2, Vec2Array, Vec3, Vec3Array, Vec4, Vec4Array
- Defined in:
- lib/snow-math/to_a.rb
Overview
Provides basic support for converting Snow math objects to Ruby arrays. In addition, it also provides rudimentary support for each, map, and map! for all math types.
For example:
# Arrays of cells by column
Mat4[*(1 .. 16)].group_by {
|cell|
(cell.floor - 1) % 4
}
# Arrays of cells by row
Mat4[*(1 .. 16)].group_by {
|cell|
((cell - 1) / 4).floor
} # => { 0 => [1, 2, 3, 4],
# 1 => [5, 6, 7, 8],
# 2 => [9, 10, 11, 12],
# 3 => [13, 14, 15, 16] }
Note that these examples are only showing that you can use these types like most others that include the Enumerable module. The above examples are not sane ways to get columns or rows out of a Mat4.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterates over all elements of the object and yields them to a block.
-
#map(&block) ⇒ Object
In the first form, duplicates self and then calls map! on the duplicated object, passing the block to map!.
-
#map!(&block) ⇒ Object
In the first form, iterates over all elements of the object, yields them to the block given, and overwrites the element’s value with the value returned by the block.
-
#to_a ⇒ Object
Returns an array composed of the elements of self.
Instance Method Details
#each(&block) ⇒ Object
Iterates over all elements of the object and yields them to a block. In the second form, returns an Enumerator.
call-seq:
each { |elem| block } -> self
each -> Enumerator
56 57 58 59 60 61 62 63 |
# File 'lib/snow-math/to_a.rb', line 56 def each(&block) return to_enum(:each) unless block_given? (0 ... self.length).each { |index| yield(fetch(index)) } self end |
#map(&block) ⇒ Object
In the first form, duplicates self and then calls map! on the duplicated object, passing the block to map!.
In the second form, returns an Enumerator.
The return value of the block must be the same kind of object as was yielded to the block. So, if yielded a Vec3, the block must return a Vec3. If yielded a Numeric, it must return a Numeric.
call-seq:
map { |elem| block } -> new object
map -> Enumerator
103 104 105 106 |
# File 'lib/snow-math/to_a.rb', line 103 def map(&block) return to_enum(:map) unless block_given? self.dup.map!(&block) end |
#map!(&block) ⇒ Object
In the first form, iterates over all elements of the object, yields them to the block given, and overwrites the element’s value with the value returned by the block.
In the second form, returns an Enumerator.
The return value of the block must be the same kind of object as was yielded to the block. So, if yielded a Vec3, the block must return a Vec3. If yielded a Numeric, it must return a Numeric.
call-seq:
map! { |elem| block } -> self
map! -> Enumerator
80 81 82 83 84 85 86 87 |
# File 'lib/snow-math/to_a.rb', line 80 def map!(&block) return to_enum(:map!) unless block_given? (0 ... self.length).each { |index| store(index, yield(fetch(index))) } self end |
#to_a ⇒ Object
Returns an array composed of the elements of self.
call-seq: to_a -> new_ary
44 45 46 |
# File 'lib/snow-math/to_a.rb', line 44 def to_a (0 ... self.length).each.map { |index| fetch(index) } end |