Class: UOM::Factor
- Inherits:
-
Object
- Object
- UOM::Factor
- Defined in:
- lib/uom/factor.rb
Overview
A Factor designates a Unit mangnitude along a dimensional axis.
Instance Attribute Summary collapse
-
#abbreviation ⇒ Object
readonly
Returns the value of attribute abbreviation.
-
#converter ⇒ Object
readonly
Returns the value of attribute converter.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
Instance Method Summary collapse
-
#as(factor) ⇒ Object
Returns the multiplier which converts this Factor into the given factor.
-
#initialize(label, abbreviation, base, multiplier = nil, &converter) ⇒ Factor
constructor
Creates a Factor with the given label, abbreviations and conversion multiplier or block.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(label, abbreviation, base, multiplier = nil, &converter) ⇒ Factor
Creates a Factor with the given label, abbreviations and conversion multiplier or block. The multiplier is the amount of this factor in the base factor. For example, KILO and MILLI are defined as:
KILO = Factor.new(:kilo, :k, UNIT, 1000)
MILLI = Factor.new(:milli, :m, UNIT, .001)
The KILO
definition is the same as:
Factor.new(:kilo, :k, UNIT) { |unit| unit * 1000 }
This definition denotes that one kilo of a unit equals 1000 of the units.
22 23 24 25 26 27 28 29 30 |
# File 'lib/uom/factor.rb', line 22 def initialize(label, abbreviation, base, multiplier=nil, &converter) # :yields: factor @label = label @abbreviation = abbreviation @base = base @converter = converter @converter ||= lambda { |n| n * multiplier } if multiplier # add this Factor to the extent Factor << self end |
Instance Attribute Details
#abbreviation ⇒ Object (readonly)
Returns the value of attribute abbreviation.
12 13 14 |
# File 'lib/uom/factor.rb', line 12 def abbreviation @abbreviation end |
#converter ⇒ Object (readonly)
Returns the value of attribute converter.
12 13 14 |
# File 'lib/uom/factor.rb', line 12 def converter @converter end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
12 13 14 |
# File 'lib/uom/factor.rb', line 12 def label @label end |
Instance Method Details
#as(factor) ⇒ Object
Returns the multiplier which converts this Factor into the given factor.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/uom/factor.rb', line 33 def as(factor) if factor == self or (@converter.nil? and factor.converter.nil?) then 1.0 elsif @converter.nil? then 1.0 / factor.as(self) elsif factor == @base then @converter.call(1) else self.as(@base) * @base.as(factor) end end |
#inspect ⇒ Object
49 50 51 52 53 |
# File 'lib/uom/factor.rb', line 49 def inspect content = "#{label}" content += ", #{abbreviation}" if abbreviation "#{self.class.name}@#{self.object_id}[#{content}]" end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/uom/factor.rb', line 45 def to_s label.to_s end |