Module: Vector2d::Conversions
- Included in:
- Vector2d
- Defined in:
- lib/vector2d/conversions.rb
Overview
Conversions to and from other Ruby objects, including the coercion protocol and pattern matching. Conversions to the standard library Matrix and Vector are in Vector2d::MatrixInterop.
Instance Method Summary collapse
-
#coerce(other) ⇒ Array(self, self)
Implements Ruby's coercion protocol, so a vector can be the right hand operand of a scalar.
-
#deconstruct ⇒ Array<Integer, Float, Rational, BigDecimal>
Returns the components as an array, so a vector can be matched against an array pattern.
-
#deconstruct_keys(_keys) ⇒ Hash{Symbol => Integer, Float, Rational, BigDecimal}
Returns the components as a hash, so a vector can be matched against a hash pattern.
-
#inspect ⇒ String
Renders vector as a pretty string.
-
#to_a ⇒ Array<Integer, Float, Rational, BigDecimal>
Converts vector to array.
-
#to_f_vector ⇒ self
Converts the coordinates to floats.
-
#to_hash ⇒ Hash{Symbol => Integer, Float, Rational, BigDecimal}
Converts vector to hash.
-
#to_i_vector ⇒ self
Converts the coordinates to integers.
-
#to_s ⇒ String
Converts vector to string.
Instance Method Details
#coerce(other) ⇒ Array(self, self)
Implements Ruby's coercion protocol, so a vector can be the right hand operand of a scalar.
2 * Vector2d(3, 4) # => Vector2d(6,8)
The operand is built through #build, so a subclass is the result on either side of the operator.
Matrices are coerced the other way around, see Vector2d::MatrixInterop#coerce.
21 22 23 24 |
# File 'lib/vector2d/conversions.rb', line 21 def coerce(other) v = coerce_vector(other) [build(v.x, v.y), self] end |
#deconstruct ⇒ Array<Integer, Float, Rational, BigDecimal>
37 38 39 |
# File 'lib/vector2d/conversions.rb', line 37 def deconstruct to_a end |
#deconstruct_keys(_keys) ⇒ Hash{Symbol => Integer, Float, Rational, BigDecimal}
Returns the components as a hash, so a vector can be matched against a hash pattern. Both components are always returned, whichever keys the pattern asks for.
Vector2d(3, 4).deconstruct_keys([:x]) # => {x: 3, y: 4}
case Vector2d(0, 4)
in {x: 0} then :on_y_axis
in {y: 0} then :on_x_axis
end # => :on_y_axis
56 57 58 |
# File 'lib/vector2d/conversions.rb', line 56 def deconstruct_keys(_keys) to_hash end |
#inspect ⇒ String
Renders vector as a pretty string.
Vector2d(2, 3).inspect # => "Vector2d(2,3)"
65 66 67 |
# File 'lib/vector2d/conversions.rb', line 65 def inspect "#{self.class}(#{x},#{y})" end |
#to_a ⇒ Array<Integer, Float, Rational, BigDecimal>
Converts vector to array.
Vector2d(2, 3).to_a # => [2,3]
74 75 76 |
# File 'lib/vector2d/conversions.rb', line 74 def to_a [x, y] end |
#to_f_vector ⇒ self
Converts the coordinates to floats. As with #to_i_vector, the result is a vector of this class.
Vector2d(2, 3).to_f_vector # => Vector2d(2.0,3.0)
105 106 107 |
# File 'lib/vector2d/conversions.rb', line 105 def to_f_vector build(x.to_f, y.to_f) end |
#to_hash ⇒ Hash{Symbol => Integer, Float, Rational, BigDecimal}
Converts vector to hash.
Vector2d(2, 3).to_hash # => {x: 2, y: 3}
84 85 86 |
# File 'lib/vector2d/conversions.rb', line 84 def to_hash { x: x, y: y } end |
#to_i_vector ⇒ self
Converts the coordinates to integers. The result is still a vector of this class, unlike #to_vector, which converts to the standard library Vector.
Vector2d(2.0, 3.0).to_i_vector # => Vector2d(2,3)
95 96 97 |
# File 'lib/vector2d/conversions.rb', line 95 def to_i_vector build(x.to_i, y.to_i) end |