Class: Geometry::SizeZero

Inherits:
Object
  • Object
show all
Defined in:
lib/geometry/size_zero.rb

Overview

An object repesenting a zero Size in N-dimensional space

A SizeZero object is a Size that will always compare equal to zero and unequal to everything else, regardless of dimensionality. You can think of it as an application of the Null Object Pattern.

Unary operators collapse

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object



57
58
59
# File 'lib/geometry/size_zero.rb', line 57

def *(other)
    self
end

#+(other) ⇒ Object



45
46
47
# File 'lib/geometry/size_zero.rb', line 45

def +(other)
    other
end

#+@Object



36
37
38
# File 'lib/geometry/size_zero.rb', line 36

def +@
    self
end

#-(other) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/geometry/size_zero.rb', line 49

def -(other)
    if other.respond_to? :-@
	-other
    elsif other.respond_to? :map
	other.map {|a| -a }
    end
end

#-@Object



40
41
42
# File 'lib/geometry/size_zero.rb', line 40

def -@
    self
end

#/(other) ⇒ Object



61
62
63
64
65
# File 'lib/geometry/size_zero.rb', line 61

def /(other)
    raise OperationNotDefined unless other.is_a? Numeric
    raise ZeroDivisionError if 0 == other
    self
end

#coerce(other) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/geometry/size_zero.rb', line 21

def coerce(other)
    if other.is_a? Numeric
	[other, 0]
    elsif other.is_a? Array
	[other, Array.new(other.size,0)]
    elsif other.is_a? Vector
	[other, Vector[*Array.new(other.size,0)]]
    else
	[Size[other], Size[Array.new(other.size,0)]]
    end
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/geometry/size_zero.rb', line 12

def eql?(other)
    if other.respond_to? :all?
	other.all? {|e| e.eql? 0}
    else
	other == 0
    end
end