Class: UnitsHash

Inherits:
Hash show all
Defined in:
lib/units/units_hash.rb

Overview

The unit part of a NumericWithUnits is a UnitsHash - a Hash from UnitsUnit instances to powers.

Constant Summary collapse

@@debug =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit = nil, power = 1) ⇒ UnitsHash

:nodoc



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/units/units_hash.rb', line 13

def initialize(unit = nil,power = 1) # :nodoc
  if unit
    if unit.kind_of? UnitsUnit
      self[unit] = power
    elsif unit.kind_of? UnitsHash
      unit.each { |k,v| self[k] = v*power }
    else
      raise UnitsException.new("invalid unit: #{unit}")
    end
  end
end

Class Method Details

.debug=(value) ⇒ Object



9
10
11
# File 'lib/units/units_hash.rb', line 9

def self.debug=(value)
  @@debug = value
end

Instance Method Details

#**(power) ⇒ Object

Returns a new UnitsHash whose value is the instance raised to the given power.



56
57
58
# File 'lib/units/units_hash.rb', line 56

def **(power)
  clone.power!(power)
end

#abbrevs_to_sObject

:nodoc:



38
39
40
41
42
43
44
45
46
# File 'lib/units/units_hash.rb', line 38

def abbrevs_to_s # :nodoc:
  p = select {|k,v| v > 0}.sort{|e1,e2| e1[1] <=> e2[1]}.collect {|e|
    "#{e[0].abbrevs[0] || e[0].name}#{(e[1] == 1) ? "" : "^#{e[1]}"}"}
  n = select {|k,v| v < 0}.sort{|e1,e2| e1[1] <=> e2[1]}.collect {|e|
    "#{e[0].abbrevs[0] || e[0].name}#{(e[1] == -1) ? "" : "^#{-e[1]}"}"}
  numerator = (p.size > 0)? p.join(" ") : (n.size > 0)? "1" : ""
  denominator = (n.size > 0)? ((p.size > 0)? " / " : "/")+n.join(" ") : ""
  "#{numerator}#{denominator}"
end

#derived?Boolean

Returns true is any of the instance’s components are derived.

Returns:

  • (Boolean)


87
88
89
# File 'lib/units/units_hash.rb', line 87

def derived?
  keys.select{|unit| unit.units_system.units_measure.derived != nil}.size > 0
end

#has_units?Boolean

Return true if the instance is not equal to 1, ie. has at least one non-zero exponent.

Returns:

  • (Boolean)


108
109
110
# File 'lib/units/units_hash.rb', line 108

def has_units?
  (self.select {|k,v| v != 0.0}).size > 0
end

#measureObject

Returns the UnitsMeasure of the instance if defined.



79
80
81
82
83
84
# File 'lib/units/units_hash.rb', line 79

def measure
  measure = UnitsMeasure.new
  each { |unit,power|
    measure.merge_derivation unit.units_system.units_measure => power }
  Units.find_by_derivation(measure.derived)
end

#merge(value, power = 1) ⇒ Object Also known as: *

Returns a new UnitsHash whose value is the instance’s units and powers have been merged with the given value raised to the power.



62
63
64
# File 'lib/units/units_hash.rb', line 62

def merge(value,power=1)
  clone.merge!(value,power)
end

#merge!(value, power = 1) ⇒ Object

Merges and returns the instance after the value raised to the power has been merged into it.



68
69
70
71
72
73
74
# File 'lib/units/units_hash.rb', line 68

def merge!(value,power=1)
  value.unit.each { |k,v|
    self[k] = (self[k] || 0) + v*power
    delete k if self[k] == 0
  }
  self
end

#power!(power) ⇒ Object

Raises and returns the instance after each of its exponents has been raised by the given power.



50
51
52
# File 'lib/units/units_hash.rb', line 50

def power!(power)
  self.each { |k,v| self[k] = v*power }
end

#reduceObject

Return a NumericWithUnits that represents the instance when all derived units have been replaced with the units from which they derive.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/units/units_hash.rb', line 93

def reduce
  puts "UnitsHash:reduce #{to_s}" if @@debug
  factor = 1.0
  new_unit = UnitsHash.new
  each do |unit,power|
    puts "  #{unit} #{power}" if @@debug
    factor *= unit.equals.numeric**power
    puts "  #{factor}" if @@debug
    new_unit.merge!(unit.equals,power)
  end
  factor.unite new_unit
end

#to_s(numeric = 1, use_abbrevs = false) ⇒ Object

Returns a String reprentation of the instance. If there is only one item in the UnitsHash, whole names are rendered; otherwise abbreviations are used.



27
28
29
30
31
32
33
34
35
36
# File 'lib/units/units_hash.rb', line 27

def to_s(numeric = 1,use_abbrevs = false)
  if size == 1 &&
      (su = select {|k,v| v == 1}).size == 1 &&
      !use_abbrevs
    su = su.to_a[0][0]
    ((numeric == 1)? su.name : su.plural).gsub(/_/,' ')
  else
    abbrevs_to_s
  end
end