Class: Quanty
- Inherits:
-
Object
- Object
- Quanty
- Defined in:
- lib/oddb/util/quanty/fact.rb,
lib/oddb/util/quanty/main.rb,
lib/oddb/util/quanty/parse.rb
Overview
encoding: utf-8 parse.y, quanty/parse.rb
by Masahiro Tanaka <[email protected]>
Direct Known Subclasses
Defined Under Namespace
Constant Summary collapse
Instance Attribute Summary collapse
-
#fact ⇒ Object
readonly
Returns the value of attribute fact.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
-
#val ⇒ Object
(also: #value)
readonly
Returns the value of attribute val.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #**(n) ⇒ Object
- #+(other) ⇒ Object
- #+@ ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #adjust(other) ⇒ Object
- #coerce(other) ⇒ Object
-
#initialize(*a) ⇒ Quanty
constructor
A new instance of Quanty.
- #inspect ⇒ Object
- #to_f ⇒ Object
- #to_s ⇒ Object
- #want(unit) ⇒ Object
Constructor Details
#initialize(*a) ⇒ Quanty
Returns a new instance of Quanty.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/oddb/util/quanty/main.rb', line 21 def initialize(*a) case a.size when 1 if String === a[0] @val,@unit,@fact = 1.0, a[0], nil else @val,@unit,@fact = a[0], '', nil end when 2..3 @val,@unit,@fact = a else raise ArgumentError, 'wrong # of arguments' end unless Fact === @fact @fact = Fact.new(@unit) end end |
Instance Attribute Details
#fact ⇒ Object (readonly)
Returns the value of attribute fact.
41 42 43 |
# File 'lib/oddb/util/quanty/main.rb', line 41 def fact @fact end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
40 41 42 |
# File 'lib/oddb/util/quanty/main.rb', line 40 def unit @unit end |
#val ⇒ Object (readonly) Also known as: value
Returns the value of attribute val.
39 40 41 |
# File 'lib/oddb/util/quanty/main.rb', line 39 def val @val end |
Instance Method Details
#*(other) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/oddb/util/quanty/main.rb', line 99 def * (other) if other.kind_of?(Self) unit = other.unit unless @unit.empty? if unit.empty? unit = @unit else if /\A[A-Za-z_]/ou =~ unit unit = @unit+' '+unit else unit = @unit+' ('+unit+')' end end end Self.new( @val*other.val, unit, @fact*other.fact ) else Self.new( @val*other, @unit, @fact ) end end |
#**(n) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/oddb/util/quanty/main.rb', line 90 def **(n) if /^[A-Za-z_]+&/ou =~ @unit unit = @unit+'^'+n.to_s else unit = '('+@unit+')^'+n.to_s+'' end Self.new( @val**n, unit, @fact**n ) end |
#+(other) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/oddb/util/quanty/main.rb', line 62 def + (other) val = @val + adjust(other) if @unit=='' val else Self.new( val, @unit, @fact ) end end |
#+@ ⇒ Object
80 |
# File 'lib/oddb/util/quanty/main.rb', line 80 def +@ ; Self.new( @val, @unit, @fact ) end |
#-(other) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/oddb/util/quanty/main.rb', line 71 def - (other) val = @val - adjust(other) if @unit=='' val else Self.new( val, @unit, @fact ) end end |
#-@ ⇒ Object
81 |
# File 'lib/oddb/util/quanty/main.rb', line 81 def -@ ; Self.new( -@val, @unit, @fact ) end |
#/(other) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/oddb/util/quanty/main.rb', line 119 def / (other) if other.kind_of?(Self) unit = other.unit if unit.empty? unit = @unit else if /\A[A-Za-z_-]+((\^|\*\*)?[0-9.]+)?$/ou =~ unit unit = '/ '+unit else unit = '/ ('+unit+')' end unit = @unit+' '+unit unless @unit.empty? end Self.new( @val/other.val, unit, @fact/other.fact ) else Self.new( @val/other, @unit, @fact ) end end |
#<(other) ⇒ Object
87 |
# File 'lib/oddb/util/quanty/main.rb', line 87 def < (other); @val < adjust(other) end |
#<=(other) ⇒ Object
86 |
# File 'lib/oddb/util/quanty/main.rb', line 86 def <= (other); @val <= adjust(other) end |
#<=>(other) ⇒ Object
83 |
# File 'lib/oddb/util/quanty/main.rb', line 83 def <=> (other); @val <=> adjust(other) end |
#==(other) ⇒ Object
84 |
# File 'lib/oddb/util/quanty/main.rb', line 84 def == (other); @val == adjust(other) end |
#>(other) ⇒ Object
88 |
# File 'lib/oddb/util/quanty/main.rb', line 88 def > (other); @val > adjust(other) end |
#>=(other) ⇒ Object
85 |
# File 'lib/oddb/util/quanty/main.rb', line 85 def >= (other); @val >= adjust(other) end |
#adjust(other) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/oddb/util/quanty/main.rb', line 44 def adjust(other) if other.kind_of?(Self) unless @fact === other.fact raise "not same unit: %s != %s" % [@unit,other.unit] end other.val * ( other.fact.factor / @fact.factor ) else raise @unit + ": not null unit" unless @fact.null? other / @fact.factor end end |
#coerce(other) ⇒ Object
138 139 140 |
# File 'lib/oddb/util/quanty/main.rb', line 138 def coerce(other) [ Self.new(other), self ] end |
#inspect ⇒ Object
156 157 158 |
# File 'lib/oddb/util/quanty/main.rb', line 156 def inspect "Quanty(" + @val.to_s + ",'" + @unit + "')" end |
#to_f ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/oddb/util/quanty/main.rb', line 142 def to_f if @fact.null? @val * @fact.factor elsif @fact === RadianUnit want('radian').value else raise 'cannot convert into non-dimensional Float' end end |
#to_s ⇒ Object
152 153 154 |
# File 'lib/oddb/util/quanty/main.rb', line 152 def to_s @val.to_s + "[" + @unit + "]" end |