Class: Eymiha::Envelope
- Inherits:
-
BaseEnvelope
- Object
- BaseEnvelope
- Eymiha::Envelope
- Defined in:
- lib/eymiha/util/envelope.rb
Overview
An Envelope is the minimum envelope that will completely contain a set of values. Values may be added to an instance, and it can return the number of values considered so far and its high and low boundaries.
Envelopes can be used to generate ranges, however the result may be of limited utility if the types of values being enveloped don’t play nicely with ranges.
Instance Attribute Summary
Attributes inherited from BaseEnvelope
Instance Method Summary collapse
-
#add(value) ⇒ Object
Adds a value to the instance.
-
#contains?(value) ⇒ Boolean
(also: #===)
Returns true if the instance completely contains the argument: * value is an Envelope, its high and low are contained.
-
#high ⇒ Object
Returns the high boundary of the instance.
-
#initialize(value = nil) ⇒ Envelope
constructor
Creates and returns an instance.
-
#low ⇒ Object
Returns the low boundary of the instance.
-
#to_range ⇒ Object
Returns an inclusive range from the low to high boundaries.
-
#to_s ⇒ Object
Returns a string representation of the instance.
Methods inherited from BaseEnvelope
#raise_no_compare, #raise_no_envelope
Constructor Details
#initialize(value = nil) ⇒ Envelope
Creates and returns an instance. If an argument is given, it is passed to the set method to initialize the new instance.
54 55 56 57 |
# File 'lib/eymiha/util/envelope.rb', line 54 def initialize(value=nil) super() add(value) unless value == nil end |
Instance Method Details
#add(value) ⇒ Object
Adds a value to the instance. When
-
x is an Envelope, it is coalesced into the instance.
-
otherwise, the envelope is extened to contain the value.
-
if the value cannot be compared to the boundaries, an EnvelopeException is raised.
The modified instance is returned.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/eymiha/util/envelope.rb', line 70 def add(value) if value.kind_of? Envelope count = value.count if (count > 0) add value.high add value.low @count += (count-2) end self else begin @high = value if (@count == 0 || value > @high) @low = value if (@count == 0 || value < @low) @count += 1 self rescue raise_no_compare value end end end |
#contains?(value) ⇒ Boolean Also known as: ===
Returns true if the instance completely contains the argument:
-
value is an Envelope, its high and low are contained.
-
otherwise, the value is contained.
-
if the value cannot be compared to the boundaries, an EnvelopeException is raised.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/eymiha/util/envelope.rb', line 109 def contains?(value) if value.kind_of? Envelope (contains? value.high) && (contains? value.low) else begin (value >= low) && (value <= high) rescue raise_no_compare value end end end |
#high ⇒ Object
Returns the high boundary of the instance.
-
if there are no boundaries, an EnvelopeException is raised.
93 94 95 96 |
# File 'lib/eymiha/util/envelope.rb', line 93 def high raise_no_envelope if @count == 0 @high end |
#low ⇒ Object
Returns the low boundary of the instance.
-
if there are no boundaries, an EnvelopeException is raised.
100 101 102 103 |
# File 'lib/eymiha/util/envelope.rb', line 100 def low raise_no_envelope if @count == 0 @low end |
#to_range ⇒ Object
Returns an inclusive range from the low to high boundaries
124 125 126 |
# File 'lib/eymiha/util/envelope.rb', line 124 def to_range low..high end |
#to_s ⇒ Object
Returns a string representation of the instance.
60 61 62 63 |
# File 'lib/eymiha/util/envelope.rb', line 60 def to_s values = (count > 0)? "\n high #{high}\n low #{low}" : "" "Envelope: count #{count}#{values}" end |