Module: Vector2d::Coercions

Includes:
Contracts
Included in:
Vector2d
Defined in:
lib/vector2d/coercions.rb

Instance Method Summary collapse

Instance Method Details

#coerce(other) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/vector2d/coercions.rb', line 8

def coerce(other)
  case other
  when Vector2d
    [other, self]
  when Array, Numeric, String, Hash
    [Vector2d.parse(other), self]
  else
    raise TypeError, "#{self.class} can't be coerced into #{other.class}"
  end
end

#inspectObject



24
25
26
# File 'lib/vector2d/coercions.rb', line 24

def inspect
  "Vector2d(#{x},#{y})"
end

#to_aObject



33
34
35
# File 'lib/vector2d/coercions.rb', line 33

def to_a
  [x, y]
end

#to_f_vectorObject



60
61
62
# File 'lib/vector2d/coercions.rb', line 60

def to_f_vector
  self.class.new(x.to_f, y.to_f)
end

#to_hashObject



42
43
44
# File 'lib/vector2d/coercions.rb', line 42

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

#to_i_vectorObject



51
52
53
# File 'lib/vector2d/coercions.rb', line 51

def to_i_vector
  self.class.new(x.to_i, y.to_i)
end

#to_sObject



69
70
71
# File 'lib/vector2d/coercions.rb', line 69

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