Class: Envelope

Inherits:
BaseEnvelope show all
Defined in:
lib/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

#count

Instance Method Summary collapse

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.



52
53
54
55
# File 'lib/envelope.rb', line 52

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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/envelope.rb', line 68

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.

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/envelope.rb', line 107

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

#highObject

Returns the high boundary of the instance.

  • if there are no boundaries, an EnvelopeException is raised.



91
92
93
94
# File 'lib/envelope.rb', line 91

def high
  raise_no_envelope if @count == 0
  @high
end

#lowObject

Returns the low boundary of the instance.

  • if there are no boundaries, an EnvelopeException is raised.



98
99
100
101
# File 'lib/envelope.rb', line 98

def low 
  raise_no_envelope if @count == 0
  @low
end

#to_rangeObject

Returns an inclusive range from the low to high boundaries



122
123
124
# File 'lib/envelope.rb', line 122

def to_range
  low..high
end

#to_sObject

Returns a string representation of the instance.



58
59
60
61
# File 'lib/envelope.rb', line 58

def to_s
  values = (count > 0)? "\n  high  #{high}\n  low   #{low}" : ""
  "Envelope: count #{count}#{values}"
end