Module: Vector2d::Coercions
Instance Method Summary collapse
- #coerce(other) ⇒ Object
- #inspect ⇒ Object
- #to_a ⇒ Object
- #to_f_vector ⇒ Object
- #to_hash ⇒ Object
- #to_i_vector ⇒ Object
- #to_s ⇒ Object
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 |
#inspect ⇒ Object
24 25 26 |
# File 'lib/vector2d/coercions.rb', line 24 def inspect "Vector2d(#{x},#{y})" end |
#to_a ⇒ Object
33 34 35 |
# File 'lib/vector2d/coercions.rb', line 33 def to_a [x, y] end |
#to_f_vector ⇒ Object
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_hash ⇒ Object
42 43 44 |
# File 'lib/vector2d/coercions.rb', line 42 def to_hash { x: x, y: y } end |
#to_i_vector ⇒ Object
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_s ⇒ Object
69 70 71 |
# File 'lib/vector2d/coercions.rb', line 69 def to_s "#{x}x#{y}" end |