Class: Geometry::SizeOne
- Inherits:
-
Object
- Object
- Geometry::SizeOne
- Defined in:
- lib/geometry/size_one.rb
Overview
An object repesenting a Size of 1, in N-dimensional space
A SizeOne object is a Size that will always compare equal to one and unequal to everything else, regardless of dimensionality. It’s similar to the Null Object Pattern, but for ones.
Unary operators collapse
Enumerable collapse
-
#first(n = nil) ⇒ Object
Return the first, or first n, elements (always 0).
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #coerce(other) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
Instance Method Details
#*(other) ⇒ Object
63 64 65 66 |
# File 'lib/geometry/size_one.rb', line 63 def *(other) raise OperationNotDefined unless other.is_a? Numeric other end |
#+(other) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/geometry/size_one.rb', line 45 def +(other) if other.respond_to?(:map) other.map {|a| a + 1 } else other + 1 end end |
#+@ ⇒ Object
36 37 38 |
# File 'lib/geometry/size_one.rb', line 36 def +@ self end |
#-(other) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/geometry/size_one.rb', line 53 def -(other) if other.is_a? Numeric 1 - other elsif other.respond_to? :map other.map {|a| 1 - a } else 1 - other end end |
#-@ ⇒ Object
40 41 42 |
# File 'lib/geometry/size_one.rb', line 40 def -@ -1 end |
#/(other) ⇒ Object
68 69 70 71 72 |
# File 'lib/geometry/size_one.rb', line 68 def /(other) raise OperationNotDefined unless other.is_a? Numeric raise ZeroDivisionError if 0 == other 1 / other end |
#coerce(other) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/geometry/size_one.rb', line 21 def coerce(other) if other.is_a? Numeric [other, 1] elsif other.is_a? Array [other, Array.new(other.size,1)] elsif other.is_a? Vector [other, Vector[*Array.new(other.size,1)]] else [Size[other], Size[Array.new(other.size,1)]] end end |
#eql?(other) ⇒ Boolean Also known as: ==
12 13 14 15 16 17 18 |
# File 'lib/geometry/size_one.rb', line 12 def eql?(other) if other.respond_to? :all? other.all? {|e| e.eql? 1} else other == 1 end end |
#first(n = nil) ⇒ Object
Return the first, or first n, elements (always 0)
79 80 81 |
# File 'lib/geometry/size_one.rb', line 79 def first(n=nil) Array.new(n, 1) rescue 1 end |