Class: Statistical::Domain
- Inherits:
-
Range
- Object
- Range
- Statistical::Domain
- Includes:
- Comparable
- Defined in:
- lib/statistical/helpers.rb
Overview
If the exclusion list contains points outside of the base range these points are not validated. The user is expect to supply valid input, since this is a helper class
All instances of this class are returned frozen
This class models a mathematical domain by basing it on Ruby’s Range Does not allow for enumeration of the domain unless. The primary addition here is the addtion of an exclusion list which allows us to exclude specific points and ranges from within the domain.
Constant Summary collapse
- TYPES =
[ :left_open, :right_open, :full_open, :closed ].freeze
Instance Attribute Summary collapse
-
#:exclusions ⇒ Object
readonly
The exclusion list of points and ranges to not be included in the domain.
-
#domain_type ⇒ Object
readonly
Returns the value of attribute domain_type.
-
#exclusions ⇒ Object
readonly
Returns the value of attribute exclusions.
-
#finish ⇒ Object
readonly
Returns the value of attribute finish.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
Class Method Summary collapse
-
.[](*args) ⇒ Domain
Returns a frozen new instance.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares Domain objects with Real numeric types (Fixnum, Bignum, Float).
-
#exclude?(val) ⇒ Boolean
Find if a point value is part of the instance’s exclusion list.
-
#include?(val) ⇒ Boolean
Find if a point value is part of the domain.
-
#initialize(start, finish, domain_type, *exclusions) ⇒ Domain
constructor
Creates a new domain instance which can be one of the following types :left_open, :right_open, :full_open, :closed An exclusion list is also maintained to capture undesired points, ranges.
-
#inspect ⇒ String
(also: #to_s)
Serialize the instance.
-
#new(*args) ⇒ Domain
Returns a frozen new instance, overrides Range::new.
Constructor Details
#initialize(start, finish, domain_type, *exclusions) ⇒ Domain
Creates a new domain instance which can be one of the following types
:left_open, :right_open, :full_open, :closed
An exclusion list is also maintained to capture undesired points, ranges
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/statistical/helpers.rb', line 36 def initialize(start, finish, domain_type, *exclusions) exclusions ||= [] exclude_end = false case domain_type when :left_open @exclusions = [start, exclusions].flatten exclude_end = false when :right_open @exclusions = [exclusions, finish].flatten exclude_end = true when :full_open @exclusions = [start, exclusions, finish].flatten exclude_end = true when :closed @exclusions = [exclusions].flatten exclude_end = false else raise ArgumentError, "Invalid domain type, must be one of #{DOMAIN_TYPES}" end @start = start @finish = finish @domain_type = domain_type super(@start, @finish, exclude_end) end |
Instance Attribute Details
#:exclusions ⇒ Object (readonly)
The exclusion list of points and ranges to not be included in the domain. The list must be homogenous
16 17 18 |
# File 'lib/statistical/helpers.rb', line 16
def :exclusions
@:exclusions
end
|
#domain_type ⇒ Object (readonly)
Returns the value of attribute domain_type.
19 20 21 |
# File 'lib/statistical/helpers.rb', line 19 def domain_type @domain_type end |
#exclusions ⇒ Object (readonly)
Returns the value of attribute exclusions.
19 20 21 |
# File 'lib/statistical/helpers.rb', line 19 def exclusions @exclusions end |
#finish ⇒ Object (readonly)
Returns the value of attribute finish.
19 20 21 |
# File 'lib/statistical/helpers.rb', line 19 def finish @finish end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
19 20 21 |
# File 'lib/statistical/helpers.rb', line 19 def start @start end |
Class Method Details
.[](*args) ⇒ Domain
Returns a frozen new instance
73 74 75 |
# File 'lib/statistical/helpers.rb', line 73 def self.[](*args) new(*args) end |
Instance Method Details
#<=>(other) ⇒ Object
Compares Domain objects with Real numeric types (Fixnum, Bignum, Float)
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/statistical/helpers.rb', line 118 def <=>(other) case other when Fixnum, Bignum, Float return -1 if other <= @start && !include?(other) return 1 if other >= @finish && !include?(other) return 0 if include?(other) else # Not comparable return nil end end |
#exclude?(val) ⇒ Boolean
Find if a point value is part of the instance’s exclusion list
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/statistical/helpers.rb', line 81 def exclude?(val) has_val = false @exclusions.each do |e| case e when Fixnum, Bignum, Float has_val = has_val || (e == val) when Range has_val ||= e.include?(val) end end return has_val end |
#include?(val) ⇒ Boolean
Find if a point value is part of the domain
98 99 100 |
# File 'lib/statistical/helpers.rb', line 98 def include?(val) super(val) && !exclude?(val) end |
#inspect ⇒ String Also known as: to_s
Serialize the instance
106 107 108 109 |
# File 'lib/statistical/helpers.rb', line 106 def inspect return "[#{super}] - #{@exclusions}" unless @exclusions.empty? return super end |
#new(*args) ⇒ Domain
Returns a frozen new instance, overrides Range::new
66 67 68 |
# File 'lib/statistical/helpers.rb', line 66 def new(*args) super(*args).freeze end |