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

VERSION =
'0.4.1'

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Unit) initialize(value, unit, system)



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

- (Object) method_missing(name, system = nil)



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

+ (Object) default_system

Returns the value of attribute default_system



288
289
290
# File 'lib/unit/class.rb', line 288

def default_system
  @default_system
end

Instance Attribute Details

- (Object) normalized (readonly)

Returns the value of attribute normalized



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

def normalized
  @normalized
end

- (Object) system (readonly)

Returns the value of attribute system



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

def system
  @system
end

- (Object) unit (readonly)

Returns the value of attribute unit



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

def unit
  @unit
end

- (Object) value (readonly)

Returns the value of attribute value



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

def value
  @value
end

Class Method Details

+ (Object) method_name_to_unit(name)



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

+ (Object) numeric_to_unit(object, system = nil)



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/unit/class.rb', line 294

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

+ (Object) power_unit(unit, pow)



290
291
292
# File 'lib/unit/class.rb', line 290

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

+ (Object) to_unit(object, system = nil)



307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/unit/class.rb', line 307

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

- (Object) *(other)



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

- (Object) **(exp)

Raises:

  • (TypeError)


81
82
83
84
# File 'lib/unit/class.rb', line 81

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

- (Object) +(other)



71
72
73
74
75
76
77
78
79
# File 'lib/unit/class.rb', line 71

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) -(other)



86
87
88
89
90
91
92
93
94
# File 'lib/unit/class.rb', line 86

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) -@



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

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

- (Object) /(other)



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

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

- (Object) <=>(other)



122
123
124
125
126
127
128
129
130
# File 'lib/unit/class.rb', line 122

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

- (Object) ==(other)



108
109
110
111
112
113
114
115
116
# File 'lib/unit/class.rb', line 108

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

- (Object) abs



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

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

- (Object) approx



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

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

- (Object) coerce(other)



189
190
191
# File 'lib/unit/class.rb', line 189

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

- (Boolean) compatible?(other) Also known as: compatible_with?

Compatible units can be added



140
141
142
# File 'lib/unit/class.rb', line 140

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

- (Boolean) dimensionless? Also known as: unitless?

Number without dimension



133
134
135
# File 'lib/unit/class.rb', line 133

def dimensionless?
  normalize.unit.empty?
end

- (Boolean) eql?(other)



118
119
120
# File 'lib/unit/class.rb', line 118

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

- (Object) in(unit)

Convert to other unit



147
148
149
150
# File 'lib/unit/class.rb', line 147

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

- (Object) in!(unit)



152
153
154
155
156
157
158
159
# File 'lib/unit/class.rb', line 152

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 #{unit.unit_string}"
  end
  result
end

- (Object) initialize_copy(other)



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

- (Object) inspect



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

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

- (Object) normalize

Converts to base units



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

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

- (Object) normalize!

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

- (Object) round(precision = 0)



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

def round(precision = 0)
  Unit.new(RUBY_VERSION > '1.9' ? value.round(precision) : ((value * 10**precision).round.to_f / 10**precision), unit, system)
end

- (Object) to_f



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

def to_f
  @value.to_f
end

- (Object) to_i



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

def to_i
  @value.to_i
end

- (Object) to_s



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

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

- (Object) to_tex



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

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

- (Object) unit_string(sep = '·')



193
194
195
196
# File 'lib/unit/class.rb', line 193

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

- (Boolean) zero?



104
105
106
# File 'lib/unit/class.rb', line 104

def zero?
  value.zero?
end