Class: Quanty

Inherits:
Object
  • Object
show all
Defined in:
lib/quanty/fact.rb,
lib/quanty.rb,
lib/quanty/main.rb,
lib/quanty/parse.rb

Overview

parse.y, quanty/parse.rb

by Masahiro Tanaka <[email protected]>

Defined Under Namespace

Classes: Fact, Parse

Constant Summary collapse

VERSION =
'1.2.0'
Self =
self
RadianUnit =
Quanty::Fact.new('radian')

Instance Attribute Summary collapse

Instance Method Summary collapse

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/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

#factObject (readonly)

Returns the value of attribute fact.



40
41
42
# File 'lib/quanty/main.rb', line 40

def fact
  @fact
end

#unitObject (readonly)

Returns the value of attribute unit.



39
40
41
# File 'lib/quanty/main.rb', line 39

def unit
  @unit
end

#valObject (readonly) Also known as: value

Returns the value of attribute val.



38
39
40
# File 'lib/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/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/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/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/quanty/main.rb', line 79

def +@ ; Self.new(  @val, @unit, @fact ) end

#-(other) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/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/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/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/quanty/main.rb', line 86

def  <  (other); @val  <  adjust(other) end

#<=(other) ⇒ Object



85
# File 'lib/quanty/main.rb', line 85

def  <= (other); @val  <= adjust(other) end

#<=>(other) ⇒ Object



82
# File 'lib/quanty/main.rb', line 82

def <=> (other); @val <=> adjust(other) end

#==(other) ⇒ Object



83
# File 'lib/quanty/main.rb', line 83

def  == (other); @val  == adjust(other) end

#>(other) ⇒ Object



87
# File 'lib/quanty/main.rb', line 87

def  >  (other); @val  >  adjust(other) end

#>=(other) ⇒ Object



84
# File 'lib/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/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/quanty/main.rb', line 137

def coerce(other)
  [ Self.new(other), self ]
end

#inspectObject



160
161
162
# File 'lib/quanty/main.rb', line 160

def inspect
  "Quanty(" + @val.to_s + ",'" + @unit + "')"
end

#to_fObject



146
147
148
149
150
151
152
153
154
# File 'lib/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_sObject



156
157
158
# File 'lib/quanty/main.rb', line 156

def to_s
  @val.to_s + "[" + @unit + "]"
end

#to_siObject Also known as: to_SI



141
142
143
# File 'lib/quanty/main.rb', line 141

def to_si
  @fact.to_quanty * @val
end

#want(unit) ⇒ Object



55
56
57
58
59
# File 'lib/quanty/main.rb', line 55

def want(unit)
  obj = Self.new(unit)
  val = obj.adjust(self)
  Self.new( val, unit, obj.fact )
end