Class: NumericWithUnit
- Inherits:
-
Object
show all
- Includes:
- Comparable
- Defined in:
- lib/numeric_with_unit.rb,
lib/numeric_with_unit.rb,
lib/numeric_with_unit/unit.rb,
lib/numeric_with_unit/unit.rb,
lib/numeric_with_unit/util.rb,
lib/numeric_with_unit/util2.rb,
lib/numeric_with_unit/util2.rb,
lib/numeric_with_unit/core_ext.rb,
lib/numeric_with_unit/unit_definition/base.rb,
lib/numeric_with_unit/unit_definition/common.rb,
lib/numeric_with_unit/unit_definition/deprecated.rb
Defined Under Namespace
Modules: CORE_EXT, NumUtil
Classes: DimensionError, Unit
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
10
11
12
13
|
# File 'lib/numeric_with_unit.rb', line 10
def initialize(value, unit)
@value = value
@unit = unit.is_a?(Unit) ? unit : Unit[unit]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/numeric_with_unit/util2.rb', line 10
def method_missing(name, *args)
if args.empty?
unit_str = name.to_s.gsub('_', '/')
resolve_unit_chain(Unit[unit_str])
else
raise Unit::NoUnitError
end
rescue Unit::NoUnitError
super
end
|
Instance Attribute Details
#unit ⇒ Object
Returns the value of attribute unit.
8
9
10
|
# File 'lib/numeric_with_unit.rb', line 8
def unit
@unit
end
|
#value ⇒ Object
Returns the value of attribute value.
8
9
10
|
# File 'lib/numeric_with_unit.rb', line 8
def value
@value
end
|
Instance Method Details
#*(other) ⇒ Object
103
104
105
106
107
108
109
110
|
# File 'lib/numeric_with_unit.rb', line 103
def *(other)
case other
when self.class
multiply_with_other_unit(other)
else
self.class.new(@value*other, @unit)
end
end
|
#**(num) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/numeric_with_unit.rb', line 129
def **(num)
if @unit.derivation.all?{|k,v| o = v * num; o.to_i == o}
self.class.new(@value**num, @unit**num)
else
nu = @unit.simplify
if nu.derivation.all?{|k,v| o = v * num; o.to_i == o}
nv = nu.from_si(@unit.to_si(@value))
self.class.new(nv ** num, nu**num)
else
raise DimensionError, "All derivating units order multiplied #{num} must be integer"
end
end
end
|
#+(other) ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/numeric_with_unit.rb', line 90
def +(other)
nwu = if other.is_a? self.class
other
else
self.class.new(other, Unit.new)
end
add_with_other_unit(nwu)
end
|
#+@ ⇒ Object
82
83
84
|
# File 'lib/numeric_with_unit.rb', line 82
def +@
self
end
|
#-(other) ⇒ Object
99
100
101
|
# File 'lib/numeric_with_unit.rb', line 99
def -(other)
self + (-other)
end
|
#-@ ⇒ Object
86
87
88
|
# File 'lib/numeric_with_unit.rb', line 86
def -@
self.class.new(-@value, @unit)
end
|
#/(other) ⇒ Object
112
113
114
115
116
117
118
119
|
# File 'lib/numeric_with_unit.rb', line 112
def /(other)
case other
when self.class
devide_with_other_unit(other)
else
self.class.new(@value/other, @unit)
end
end
|
#<=>(other) ⇒ Object
If ohter is NumericWithUnit and same dimension, comparing value with converting to si. Else return nil.
27
28
29
30
31
|
# File 'lib/numeric_with_unit.rb', line 27
def <=>(other)
if other.is_a?(self.class) and @unit.dimension_equal? other.unit
value_si <=> other.value_si
end
end
|
#===(other) ⇒ Object
33
34
35
|
# File 'lib/numeric_with_unit.rb', line 33
def ===(other)
self.<=>(other) == 0
end
|
#cbrt ⇒ Object
148
|
# File 'lib/numeric_with_unit.rb', line 148
def cbrt; root(3) end
|
#ceil ⇒ Object
150
151
152
|
# File 'lib/numeric_with_unit.rb', line 150
def ceil
self.class.new(@value.ceil, @unit)
end
|
#coerce(other) ⇒ Object
121
122
123
124
125
126
127
|
# File 'lib/numeric_with_unit.rb', line 121
def coerce(other)
if other.is_a?(self.class)
[other, self]
else
[self.class.new(other, Unit.new), self]
end
end
|
#convert(unit) ⇒ Object
Also known as:
to_nwu
71
72
73
|
# File 'lib/numeric_with_unit.rb', line 71
def convert(unit)
clone.convert!(unit)
end
|
#convert!(unit) ⇒ Object
Also known as:
[]
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/numeric_with_unit.rb', line 58
def convert!(unit)
new_unit = unit.is_a?(Unit) ? unit : Unit[unit]
unless @unit.dimension_equal? new_unit
raise DimensionError, "Dimensions are different between #{@unit.symbol}#{@unit.dimension} #{new_unit.symbol}#{new_unit.dimension}"
end
new_value = new_unit.from_si(@unit.to_si(@value))
@value, @unit = new_value, new_unit
self
end
|
#floor ⇒ Object
154
155
156
|
# File 'lib/numeric_with_unit.rb', line 154
def floor
self.class.new(@value.floor, @unit)
end
|
#inspect ⇒ Object
Return String for inspect
16
17
18
|
# File 'lib/numeric_with_unit.rb', line 16
def inspect
"#{@value.inspect} [#{@unit.symbol}] #{@unit.dimension.inspect}"
end
|
#root(num) ⇒ Object
144
145
146
|
# File 'lib/numeric_with_unit.rb', line 144
def root(num)
self**(Rational(1,num))
end
|
#round ⇒ Object
158
159
160
|
# File 'lib/numeric_with_unit.rb', line 158
def round
self.class.new(@value.round, @unit)
end
|
#simplify ⇒ Object
77
78
79
|
# File 'lib/numeric_with_unit.rb', line 77
def simplify
convert(@unit.simplify)
end
|
#sqrt ⇒ Object
147
|
# File 'lib/numeric_with_unit.rb', line 147
def sqrt; root(2) end
|
#succ ⇒ Object
Return succed value with same unit.
38
39
40
|
# File 'lib/numeric_with_unit.rb', line 38
def succ
self.class.new(@value.succ, @unit)
end
|
#to_f ⇒ Object
48
49
50
|
# File 'lib/numeric_with_unit.rb', line 48
def to_f
@value.to_f
end
|
#to_i ⇒ Object
43
44
45
|
# File 'lib/numeric_with_unit.rb', line 43
def to_i
@value.to_i
end
|
#to_s ⇒ Object
Return String with value and unit symbol
21
22
23
|
# File 'lib/numeric_with_unit.rb', line 21
def to_s
"#{@value.to_s} #{@unit.symbol}"
end
|
#truncate ⇒ Object
162
163
164
|
# File 'lib/numeric_with_unit.rb', line 162
def truncate
self.class.new(@value.truncate, @unit)
end
|
#value_si ⇒ Object
53
54
55
|
# File 'lib/numeric_with_unit.rb', line 53
def value_si
@unit.to_si(@value)
end
|