class NumericWithUnit
Attributes
unit[R]
unit_chain[W]
value[R]
Public Class Methods
new(value, unit)
click to toggle source
# File lib/numeric_with_unit.rb, line 10 def initialize(value, unit) @value = value @unit = unit.is_a?(Unit) ? unit : Unit[unit] end
Public Instance Methods
*(other)
click to toggle source
# 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)
click to toggle source
# File lib/numeric_with_unit.rb, line 129 def **(num) # Dimension Check if @unit.derivation.all?{|k,v| o = v * num; o.to_i == o} # TODO: 整数かどうかの判定方法いいのこれで 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)
click to toggle source
# 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
+@()
click to toggle source
# File lib/numeric_with_unit.rb, line 82 def +@ self end
-(other)
click to toggle source
# File lib/numeric_with_unit.rb, line 99 def -(other) self + (-other) end
-@()
click to toggle source
# File lib/numeric_with_unit.rb, line 86 def -@ self.class.new(-@value, @unit) end
/(other)
click to toggle source
# 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)
click to toggle source
If ohter is NumericWithUnit
and same dimension, comparing value with converting to si. Else return nil.
# 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)
click to toggle source
# File lib/numeric_with_unit.rb, line 33 def ===(other) self.<=>(other) == 0 end
cbrt()
click to toggle source
# File lib/numeric_with_unit.rb, line 148 def cbrt; root(3) end
ceil()
click to toggle source
# File lib/numeric_with_unit.rb, line 150 def ceil self.class.new(@value.ceil, @unit) end
coerce(other)
click to toggle source
# 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)
click to toggle source
# File lib/numeric_with_unit.rb, line 71 def convert(unit) clone.convert!(unit) end
Also aliased as: to_nwu
convert!(unit)
click to toggle source
Convert to given unit
# 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
Also aliased as: []
floor()
click to toggle source
# File lib/numeric_with_unit.rb, line 154 def floor self.class.new(@value.floor, @unit) end
inspect()
click to toggle source
Return String for inspect
# File lib/numeric_with_unit.rb, line 16 def inspect "#{@value.inspect} [#{@unit.symbol}] #{@unit.dimension.inspect}" end
method_missing(name, *args)
click to toggle source
Calls superclass method
# 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
root(num)
click to toggle source
# File lib/numeric_with_unit.rb, line 144 def root(num) self**(Rational(1,num)) end
round()
click to toggle source
# File lib/numeric_with_unit.rb, line 158 def round self.class.new(@value.round, @unit) end
simplify()
click to toggle source
Convert to simple unit
# File lib/numeric_with_unit.rb, line 77 def simplify convert(@unit.simplify) end
sqrt()
click to toggle source
# File lib/numeric_with_unit.rb, line 147 def sqrt; root(2) end
succ()
click to toggle source
Return succed value with same unit.
# File lib/numeric_with_unit.rb, line 38 def succ self.class.new(@value.succ, @unit) end
to_f()
click to toggle source
Return value.to_f
# File lib/numeric_with_unit.rb, line 48 def to_f @value.to_f end
to_i()
click to toggle source
Return value.to_i
# File lib/numeric_with_unit.rb, line 43 def to_i @value.to_i end
to_s()
click to toggle source
Return String with value and unit symbol
# File lib/numeric_with_unit.rb, line 21 def to_s "#{@value.to_s} #{@unit.symbol}" end
truncate()
click to toggle source
# File lib/numeric_with_unit.rb, line 162 def truncate self.class.new(@value.truncate, @unit) end
value_si()
click to toggle source
Return value in si
# File lib/numeric_with_unit.rb, line 53 def value_si @unit.to_si(@value) end
Private Instance Methods
add_with_other_unit(other)
click to toggle source
# File lib/numeric_with_unit.rb, line 169 def add_with_other_unit(other) if @unit.dimension_equal? other.unit v1 = @unit.to_si(@value) v2 = other.unit.to_si(other.value) vr = @unit.from_si(v1+v2) self.class.new(vr, @unit) else raise DimensionError, "Dimensions are different between #{@unit.dimension} #{other.unit.dimension}" end end
adjust_other_unit(other)
click to toggle source
なるべくselfと同じ単位を使用するようにotherを変換します。
# File lib/numeric_with_unit.rb, line 191 def adjust_other_unit(other) if @unit.derivation.any?{|k,v| k == other.unit} # [L/min]*[min]などのケース other elsif h = @unit.derivation.find{|k,v| k.dimension_equal? other.unit} # [L/min]*[s]などのケース other[ h.first ] elsif @unit.dimension_equal? other.unit # [mm]*[cm]などのケース other[@unit] else other end end
devide_with_other_unit(other)
click to toggle source
# File lib/numeric_with_unit.rb, line 185 def devide_with_other_unit(other) onwu = adjust_other_unit(other) self.class.new(@value / onwu.value, @unit / onwu.unit) end
multiply_with_other_unit(other)
click to toggle source
# File lib/numeric_with_unit.rb, line 180 def multiply_with_other_unit(other) onwu = adjust_other_unit(other) self.class.new(@value * onwu.value, @unit * onwu.unit) end
resolve_unit_chain(unit)
click to toggle source
# File lib/numeric_with_unit/util2.rb, line 25 def resolve_unit_chain(unit) unit_chain = @unit_chain || [] unit_chain.map!{|nwu, chained_unit| [nwu, chained_unit * unit]} unit_chain << [self, unit] if i = unit_chain.index{|nwu, chained_unit| nwu.unit.dimension_equal? chained_unit} nwu, chained_unit = *unit_chain[i] nwu.convert(chained_unit) else newnwu = self.class.new(@value, @unit*unit) newnwu.unit_chain = unit_chain newnwu end end