Class: Dimensional::Dimension

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#basisObject (readonly)

Returns the value of attribute basis.



32
33
34
# File 'lib/dimensional/dimension.rb', line 32

def basis
  @basis
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/dimensional/dimension.rb', line 32

def name
  @name
end

#symbolObject (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.

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


44
45
46
# File 'lib/dimensional/dimension.rb', line 44

def fundamental?
  basis.empty?
end

#hashObject

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_sObject



65
66
67
# File 'lib/dimensional/dimension.rb', line 65

def to_s
  name rescue super
end

#to_symObject



69
70
71
# File 'lib/dimensional/dimension.rb', line 69

def to_sym
  symbol.to_sym
end