Class: Unit

Inherits:
Numeric show all
Defined in:
lib/unit/dsl.rb,
lib/unit/class.rb,
lib/unit/system.rb,
lib/unit/version.rb

Defined Under Namespace

Classes: IncompatibleUnitError, System

Constant Summary collapse

VERSION =
'0.4.0'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, unit, system) ⇒ Unit

Returns a new instance of Unit.



7
8
9
10
11
12
13
# File 'lib/unit/class.rb', line 7

def initialize(value, unit, system)
  @system = system
  @value = value
  @unit = unit.dup
  @normalized = nil
  reduce!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, system = nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/unit/dsl.rb', line 16

def method_missing(name, system = nil)
  if name.to_s =~ /^in_(.*?)(!?)$/
    unit = Unit.method_name_to_unit($1)
    $2.empty? ? self.in(unit) : self.in!(unit)
  else
    super(name, system || @system)
  end
end

Class Attribute Details

.default_systemObject

Returns the value of attribute default_system.



280
281
282
# File 'lib/unit/class.rb', line 280

def default_system
  @default_system
end

Instance Attribute Details

#normalizedObject (readonly)

Returns the value of attribute normalized.



3
4
5
# File 'lib/unit/class.rb', line 3

def normalized
  @normalized
end

#systemObject (readonly)

Returns the value of attribute system.



3
4
5
# File 'lib/unit/class.rb', line 3

def system
  @system
end

#unitObject (readonly)

Returns the value of attribute unit.



3
4
5
# File 'lib/unit/class.rb', line 3

def unit
  @unit
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/unit/class.rb', line 3

def value
  @value
end

Class Method Details

.method_name_to_unit(name) ⇒ Object



12
13
14
# File 'lib/unit/dsl.rb', line 12

def self.method_name_to_unit(name)
  name.to_s.sub(/^per_/, '1/').gsub('_per_', '/').gsub('_', ' ')
end

.numeric_to_unit(object, system = nil) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/unit/class.rb', line 286

def numeric_to_unit(object, system = nil)
  system ||= Unit.default_system
  case object
  when Unit
    raise IncompatibleUnitError, "Unit system of #{object.inspect} is incompatible with #{system.name}" if object.system != system
    object
  when Numeric
    Unit.new(object, [], system)
  else
    raise TypeError, "#{object.class} can't be coerced into Unit"
  end
end

.power_unit(unit, pow) ⇒ Object



282
283
284
# File 'lib/unit/class.rb', line 282

def power_unit(unit, pow)
  unit.map {|factor, name, exp| [factor, name, exp * pow] }
end

.to_unit(object, system = nil) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/unit/class.rb', line 299

def to_unit(object, system = nil)
  system ||= Unit.default_system
  case object
  when String, Symbol
    unit = system.parse_unit(object.to_s)
    system.validate_unit(unit)
    Unit.new(1, unit, system)
  when Array
    system.validate_unit(object)
    Unit.new(1, object, system)
  else
    numeric_to_unit(object, system)
  end
end

Instance Method Details

#*(other) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/unit/class.rb', line 48

def *(other)
  if Numeric === other
    other = coerce_numeric(other)
    Unit.new(other.value * self.value, other.unit + self.unit, system)
  else
    apply_through_coercion(other, __method__)
  end
end

#**(exp) ⇒ Object

Raises:

  • (TypeError)


78
79
80
81
# File 'lib/unit/class.rb', line 78

def **(exp)
  raise TypeError if Unit === exp
  Unit.new(value ** exp, Unit.power_unit(unit, exp), system)
end

#+(other) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/unit/class.rb', line 68

def +(other)
  if Numeric === other
    other = coerce_numeric_compatible(other)
    a, b = self.normalize, other.normalize
    Unit.new(a.value + b.value, b.unit, system).in(self)
  else
    apply_through_coercion(other, __method__)
  end
end

#-(other) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/unit/class.rb', line 83

def -(other)
  if Numeric === other
    other = coerce_numeric_compatible(other)
    a, b = self.normalize, other.normalize
    Unit.new(a.value - b.value, b.unit, system).in(self)
  else
    apply_through_coercion(other, __method__)
  end
end

#-@Object



93
94
95
# File 'lib/unit/class.rb', line 93

def -@
  Unit.new(-value, unit, system)
end

#/(other) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/unit/class.rb', line 57

def /(other)
  if Numeric === other
    other = coerce_numeric(other)
    Unit.new(Integer === value && Integer === other.value ?
             Rational(value, other.value) : value / other.value,
             unit + Unit.power_unit(other.unit, -1), system)
  else
    apply_through_coercion(other, __method__)
  end
end

#<=>(other) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/unit/class.rb', line 119

def <=>(other)
  if Numeric === other
    other = coerce_numeric_compatible(other)
    a, b = self.normalize, other.normalize
    a.value <=> b.value
  else
    apply_through_coercion(other, __method__)
  end
end

#==(other) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/unit/class.rb', line 105

def ==(other)
  if Numeric === other
    other = coerce_numeric(other)
    a, b = self.normalize, other.normalize
    a.value == b.value && a.unit == b.unit
  else
    apply_through_coercion(other, __method__)
  end
end

#absObject



97
98
99
# File 'lib/unit/class.rb', line 97

def abs
  Unit.new(value.abs, unit, system)
end

#approxObject



178
179
180
# File 'lib/unit/class.rb', line 178

def approx
  Unit.new(self.to_f, unit, system)
end

#coerce(other) ⇒ Object



182
183
184
# File 'lib/unit/class.rb', line 182

def coerce(other)
  [coerce_numeric(other), self]
end

#compatible?(other) ⇒ Boolean Also known as: compatible_with?

Compatible units can be added

Returns:

  • (Boolean)


137
138
139
# File 'lib/unit/class.rb', line 137

def compatible?(other)
  self.normalize.unit == Unit.to_unit(other, system).normalize.unit
end

#dimensionless?Boolean Also known as: unitless?

Number without dimension

Returns:

  • (Boolean)


130
131
132
# File 'lib/unit/class.rb', line 130

def dimensionless?
  normalize.unit.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/unit/class.rb', line 115

def eql?(other)
  Unit === other && value.eql?(other.value) && unit == other.unit
end

#in(unit) ⇒ Object

Convert to other unit



144
145
146
147
# File 'lib/unit/class.rb', line 144

def in(unit)
  conversion = Unit.new(1, Unit.to_unit(unit, system).unit, system)
  (self / conversion).normalize * conversion
end

#in!(unit) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/unit/class.rb', line 149

def in!(unit)
  unit = coerce_object(unit)
  result = self.in(unit)
  unless result.unit == unit.unit
    raise TypeError, "Unexpected #{result.inspect}, expected to be in #{other_unit.unit_string}"
  end
  result
end

#initialize_copy(other) ⇒ Object



15
16
17
18
19
20
# File 'lib/unit/class.rb', line 15

def initialize_copy(other)
  @system = other.system
  @value = other.value
  @unit = other.unit.dup
  @normalized = other.normalized
end

#inspectObject



158
159
160
# File 'lib/unit/class.rb', line 158

def inspect
  unit.empty? ? %{Unit("#{value}")} : %{Unit("#{value} #{unit_string('.')}")}
end

#normalizeObject

Converts to base units



23
24
25
# File 'lib/unit/class.rb', line 23

def normalize
  @normalized ||= dup.normalize!
end

#normalize!Object

Converts to base units



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/unit/class.rb', line 28

def normalize!
  if @normalized != self
    begin
      last_unit = @unit
      @unit = []
      last_unit.each do |factor, unit, exp|
        @value *= @system.factor[factor][:value] ** exp if factor != :one
        if Numeric === unit
          @unit << [:one, unit, exp]
        else
          @unit += Unit.power_unit(@system.unit[unit][:def], exp)
        end
      end
    end while last_unit != @unit
    reduce!
    @normalized = self
  end
  self
end

#to_fObject



174
175
176
# File 'lib/unit/class.rb', line 174

def to_f
  @value.to_f
end

#to_iObject



170
171
172
# File 'lib/unit/class.rb', line 170

def to_i
  @value.to_i
end

#to_sObject



162
163
164
# File 'lib/unit/class.rb', line 162

def to_s
  unit.empty? ? value.to_s : "#{value} #{unit_string}"
end

#to_texObject



166
167
168
# File 'lib/unit/class.rb', line 166

def to_tex
  unit.empty? ? value.to_s : "\SI{#{value}}{#{unit_string('.')}}"
end

#unit_string(sep = '·') ⇒ Object



186
187
188
189
# File 'lib/unit/class.rb', line 186

def unit_string(sep = '·')
  (unit_list(@unit.select {|factor, name, exp| exp >= 0 }) +
   unit_list(@unit.select {|factor, name, exp| exp < 0 })).join(sep)
end

#zero?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/unit/class.rb', line 101

def zero?
  value.zero?
end