Class: Dimensional::Dimension
- Inherits:
-
Object
- Object
- Dimensional::Dimension
- Defined in:
- lib/dimensional/dimension.rb
Overview
Represents a dimension that can be used to characterize a physical quantity. With the exception of certain fundamental dimensions, all dimensions are expressed as a set of exponents relative to the fundamentals. Reference: en.wikipedia.org/wiki/Dimensional_analysis
Instance Attribute Summary collapse
-
#basis ⇒ Object
readonly
Returns the value of attribute basis.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Class Method Summary collapse
-
.[](sym) ⇒ Object
Lookup the dimension by name or symbol.
- .register(*args) ⇒ Object
-
.reset! ⇒ Object
Purge all dimensions from storage.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Equality is determined by equality of value-ish attributes.
-
#eql?(other) ⇒ Boolean
Hashing collisions are desired when we have same identity-defining attributes.
- #fundamental? ⇒ Boolean (also: #base?)
-
#hash ⇒ Object
This is pretty lame, but the expected usage means we shouldn’t get penalized.
-
#initialize(name, symbol = nil, basis = {}) ⇒ Dimension
constructor
A new instance of Dimension.
- #to_s ⇒ Object
- #to_sym ⇒ Object
Constructor Details
#initialize(name, symbol = nil, basis = {}) ⇒ Dimension
Returns a new instance of Dimension.
34 35 36 37 38 39 40 41 42 |
# File 'lib/dimensional/dimension.rb', line 34 def initialize(name, symbol = nil, basis = {}) basis.each_pair do |k,v| raise "Invalid fundamental dimension #{k}" unless k.fundamental? raise "Invalid exponent for basis member #{v}" unless v.kind_of?(Integer) # Can't this really be any Rational? end @basis = Hash.new(0).merge(basis) @name = name.to_s @symbol = symbol.nil? ? name.to_s.slice(0, 1).upcase : symbol.to_s end |
Instance Attribute Details
#basis ⇒ Object (readonly)
Returns the value of attribute basis.
32 33 34 |
# File 'lib/dimensional/dimension.rb', line 32 def basis @basis end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
32 33 34 |
# File 'lib/dimensional/dimension.rb', line 32 def name @name end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
32 33 34 |
# File 'lib/dimensional/dimension.rb', line 32 def symbol @symbol end |
Class Method Details
.[](sym) ⇒ Object
Lookup the dimension by name or symbol
20 21 22 23 |
# File 'lib/dimensional/dimension.rb', line 20 def self.[](sym) return nil unless sym = sym && sym.to_sym @registry[sym] || @symbol_registry[sym] end |
.register(*args) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/dimensional/dimension.rb', line 10 def self.register(*args) d = new(*args) raise "Dimension #{d}'s symbol already exists" if @symbol_registry[d.symbol] @registry[d.name.to_sym] = d @symbol_registry[d.symbol.to_sym] = d const_set(d.symbol.to_s, d) rescue nil # Not all symbols strings are valid constant names d end |
.reset! ⇒ Object
Purge all dimensions from storage.
26 27 28 29 30 |
# File 'lib/dimensional/dimension.rb', line 26 def self.reset! constants.each {|d| remove_const(d)} @registry.clear @symbol_registry.clear end |
Instance Method Details
#==(other) ⇒ Object
Equality is determined by equality of value-ish attributes. Specifically, equal basis for non-fundamental units and identicality for fundamental units. The nil dimension is inherently un-equal to any non-nil dimension.
51 52 53 |
# File 'lib/dimensional/dimension.rb', line 51 def ==(other) other.kind_of?(self.class) && ((fundamental? && other.fundamental?) ? eql?(other) : other.basis == basis) end |
#eql?(other) ⇒ Boolean
Hashing collisions are desired when we have same identity-defining attributes.
56 57 58 |
# File 'lib/dimensional/dimension.rb', line 56 def eql?(other) other.kind_of?(self.class) && other.name.eql?(self.name) end |
#fundamental? ⇒ Boolean Also known as: base?
44 45 46 |
# File 'lib/dimensional/dimension.rb', line 44 def fundamental? basis.empty? end |
#hash ⇒ Object
This is pretty lame, but the expected usage means we shouldn’t get penalized
61 62 63 |
# File 'lib/dimensional/dimension.rb', line 61 def hash [self.class, name].hash end |
#to_s ⇒ Object
65 66 67 |
# File 'lib/dimensional/dimension.rb', line 65 def to_s name rescue super end |
#to_sym ⇒ Object
69 70 71 |
# File 'lib/dimensional/dimension.rb', line 69 def to_sym symbol.to_sym end |