Class: NumericWithUnit
- Inherits:
-
Object
- Object
- NumericWithUnit
- 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/unit_definition/base.rb,
lib/numeric_with_unit/unit_definition/common.rb,
lib/numeric_with_unit/unit_definition/deprecated.rb
Defined Under Namespace
Modules: NumUtil Classes: DimensionError, Unit
Instance Attribute Summary collapse
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #**(num) ⇒ Object
- #+(other) ⇒ Object
- #+@ ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
-
#<=>(other) ⇒ Object
If ohter is NumericWithUnit and same dimension, comparing value with converting to si.
- #===(other) ⇒ Object
-
#cbrt ⇒ Object
立方根.
- #ceil ⇒ Object
- #coerce(other) ⇒ Object
-
#convert(unit) ⇒ Object
(also: #to_nwu, #[])
Convert to given unit.
- #floor ⇒ Object
-
#initialize(value, unit) ⇒ NumericWithUnit
constructor
A new instance of NumericWithUnit.
-
#inspect ⇒ Object
Return String for inspect.
- #method_missing(name, *args) ⇒ Object
- #root(num) ⇒ Object
- #round ⇒ Object
-
#simplify ⇒ Object
Convert to simple unit.
-
#sqrt ⇒ Object
平方根.
-
#succ ⇒ Object
Return succed value with same unit.
-
#to_f ⇒ Object
Return value.to_f.
-
#to_i ⇒ Object
Return value.to_i.
-
#to_s ⇒ Object
Return String with value and unit symbol.
- #truncate ⇒ Object
-
#value_si ⇒ Object
Return value in si.
Constructor Details
#initialize(value, unit) ⇒ NumericWithUnit
Returns a new instance of NumericWithUnit.
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 (readonly)
Returns the value of attribute unit.
8 9 10 |
# File 'lib/numeric_with_unit.rb', line 8 def unit @unit end |
#value ⇒ Object (readonly)
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
98 99 100 101 102 103 104 105 |
# File 'lib/numeric_with_unit.rb', line 98 def *(other) case other when self.class multiply_with_other_unit(other) else self.class.new(@value*other, @unit) end end |
#**(num) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/numeric_with_unit.rb', line 124 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) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/numeric_with_unit.rb', line 85 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
77 78 79 |
# File 'lib/numeric_with_unit.rb', line 77 def +@ self end |
#-(other) ⇒ Object
94 95 96 |
# File 'lib/numeric_with_unit.rb', line 94 def -(other) self + (-other) end |
#-@ ⇒ Object
81 82 83 |
# File 'lib/numeric_with_unit.rb', line 81 def -@ self.class.new(-@value, @unit) end |
#/(other) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/numeric_with_unit.rb', line 107 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
立方根
143 |
# File 'lib/numeric_with_unit.rb', line 143 def cbrt; root(3) end |
#ceil ⇒ Object
145 146 147 |
# File 'lib/numeric_with_unit.rb', line 145 def ceil self.class.new(@value.ceil, @unit) end |
#coerce(other) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/numeric_with_unit.rb', line 116 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, []
Convert to given unit
58 59 60 61 62 63 64 65 66 67 |
# 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)) self.class.new(new_value, new_unit) end |
#floor ⇒ Object
149 150 151 |
# File 'lib/numeric_with_unit.rb', line 149 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
139 140 141 |
# File 'lib/numeric_with_unit.rb', line 139 def root(num) self**(Rational(1,num)) end |
#round ⇒ Object
153 154 155 |
# File 'lib/numeric_with_unit.rb', line 153 def round self.class.new(@value.round, @unit) end |
#simplify ⇒ Object
Convert to simple unit
72 73 74 |
# File 'lib/numeric_with_unit.rb', line 72 def simplify convert(@unit.simplify) end |
#sqrt ⇒ Object
平方根
142 |
# File 'lib/numeric_with_unit.rb', line 142 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
Return value.to_f
48 49 50 |
# File 'lib/numeric_with_unit.rb', line 48 def to_f @value.to_f end |
#to_i ⇒ Object
Return value.to_i
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
157 158 159 |
# File 'lib/numeric_with_unit.rb', line 157 def truncate self.class.new(@value.truncate, @unit) end |
#value_si ⇒ Object
Return value in si
53 54 55 |
# File 'lib/numeric_with_unit.rb', line 53 def value_si @unit.to_si(@value) end |