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

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.

Parameters:

  • other (Vector2d, Array, String, Hash, Integer, Float, Rational, BigDecimal, ::Vector, ::Matrix)

    anything Vector2d.parse accepts

Returns:

  • (Array(self, self))

    the coerced operand and this vector



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

#deconstructArray<Integer, Float, Rational, BigDecimal>

Returns the components as an array, so a vector can be matched against an array pattern.

Vector2d(3, 4).deconstruct # => [3,4]

case Vector2d(3, 4)
in [0, 0] then :origin
in [Integer => a, Integer => b] then a + b
end # => 7

Returns:

  • (Array<Integer, Float, Rational, BigDecimal>)

    the [x, y] pair



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

Parameters:

  • _keys (Array<Symbol>, nil)

    ignored, both components are always returned

Returns:

  • (Hash{Symbol => Integer, Float, Rational, BigDecimal})

    the x and y coordinates



56
57
58
# File 'lib/vector2d/conversions.rb', line 56

def deconstruct_keys(_keys)
  to_hash
end

#inspectString

Renders vector as a pretty string.

Vector2d(2, 3).inspect # => "Vector2d(2,3)"

Returns:

  • (String)


65
66
67
# File 'lib/vector2d/conversions.rb', line 65

def inspect
  "#{self.class}(#{x},#{y})"
end

#to_aArray<Integer, Float, Rational, BigDecimal>

Converts vector to array.

Vector2d(2, 3).to_a # => [2,3]

Returns:

  • (Array<Integer, Float, Rational, BigDecimal>)

    the [x, y] pair



74
75
76
# File 'lib/vector2d/conversions.rb', line 74

def to_a
  [x, y]
end

#to_f_vectorself

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)

Returns:

  • (self)


105
106
107
# File 'lib/vector2d/conversions.rb', line 105

def to_f_vector
  build(x.to_f, y.to_f)
end

#to_hashHash{Symbol => Integer, Float, Rational, BigDecimal}

Converts vector to hash.

Vector2d(2, 3).to_hash # => {x: 2, y: 3}

Returns:

  • (Hash{Symbol => Integer, Float, Rational, BigDecimal})

    the x and y coordinates



84
85
86
# File 'lib/vector2d/conversions.rb', line 84

def to_hash
  { x: x, y: y }
end

#to_i_vectorself

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)

Returns:

  • (self)


95
96
97
# File 'lib/vector2d/conversions.rb', line 95

def to_i_vector
  build(x.to_i, y.to_i)
end

#to_sString

Converts vector to string.

Vector2d.new(150, 100).to_s # => "150x100"

Returns:

  • (String)


114
115
116
# File 'lib/vector2d/conversions.rb', line 114

def to_s
  "#{x}x#{y}"
end