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
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
- #to_si ⇒ Object (also: #to_SI)
- #want(unit) ⇒ Object
Constructor Details
#initialize(*a) ⇒ Quanty
Returns a new instance of Quanty.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/oddb/util/quanty/main.rb', line 20 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.
40 41 42 |
# File 'lib/oddb/util/quanty/main.rb', line 40 def fact @fact end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
39 40 41 |
# File 'lib/oddb/util/quanty/main.rb', line 39 def unit @unit end |
#val ⇒ Object (readonly) Also known as: value
Returns the value of attribute val.
38 39 40 |
# File 'lib/oddb/util/quanty/main.rb', line 38 def val @val end |
Instance Method Details
#*(other) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/oddb/util/quanty/main.rb', line 98 def * (other) if other.kind_of?(Self) unit = other.unit unless @unit.empty? if unit.empty? unit = @unit else if /\A[A-Za-z_]/o =~ 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
89 90 91 92 93 94 95 96 |
# File 'lib/oddb/util/quanty/main.rb', line 89 def **(n) if /^[A-Za-z_]+&/o =~ @unit unit = @unit+'^'+n.to_s else unit = '('+@unit+')^'+n.to_s+'' end Self.new( @val**n, unit, @fact**n ) end |
#+(other) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/oddb/util/quanty/main.rb', line 61 def + (other) val = @val + adjust(other) if @unit=='' val else Self.new( val, @unit, @fact ) end end |
#+@ ⇒ Object
79 |
# File 'lib/oddb/util/quanty/main.rb', line 79 def +@ ; Self.new( @val, @unit, @fact ) end |
#-(other) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/oddb/util/quanty/main.rb', line 70 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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/oddb/util/quanty/main.rb', line 118 def / (other) if other.kind_of?(Self) unit = other.unit if unit.empty? unit = @unit else if /\A[A-Za-z_-]+((\^|\*\*)?[0-9.]+)?$/o =~ 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
86 |
# File 'lib/oddb/util/quanty/main.rb', line 86 def < (other); @val < adjust(other) end |
#<=(other) ⇒ Object
85 |
# File 'lib/oddb/util/quanty/main.rb', line 85 def <= (other); @val <= adjust(other) end |
#<=>(other) ⇒ Object
82 |
# File 'lib/oddb/util/quanty/main.rb', line 82 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
87 |
# File 'lib/oddb/util/quanty/main.rb', line 87 def > (other); @val > adjust(other) end |
#>=(other) ⇒ Object
84 |
# File 'lib/oddb/util/quanty/main.rb', line 84 def >= (other); @val >= adjust(other) end |
#adjust(other) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/oddb/util/quanty/main.rb', line 43 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
137 138 139 |
# File 'lib/oddb/util/quanty/main.rb', line 137 def coerce(other) [ Self.new(other), self ] end |
#inspect ⇒ Object
160 161 162 |
# File 'lib/oddb/util/quanty/main.rb', line 160 def inspect "Quanty(" + @val.to_s + ",'" + @unit + "')" end |
#to_f ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/oddb/util/quanty/main.rb', line 146 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
156 157 158 |
# File 'lib/oddb/util/quanty/main.rb', line 156 def to_s @val.to_s + "[" + @unit + "]" end |
#to_si ⇒ Object Also known as: to_SI
141 142 143 |
# File 'lib/oddb/util/quanty/main.rb', line 141 def to_si @fact.to_quanty * @val end |