Class: Pulo::Dimension

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/pulo/quantity/dimension.rb

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ Dimension

Returns a new instance of Dimension.



6
7
8
9
# File 'lib/pulo/quantity/dimension.rb', line 6

def initialize(spec)
  @spec=spec #hash keyed on dimension name as a symbol eg :L
  clean
end

Instance Method Details

#*(val) ⇒ Object



25
26
27
# File 'lib/pulo/quantity/dimension.rb', line 25

def *(val)
  Dimension.new Hash[@spec.map{|itm| [itm[0],itm[1]*val]}]
end

#+(other_spec) ⇒ Object

def is_base?

@spec.count==1 && @spec.first[1]==1

end



18
19
20
# File 'lib/pulo/quantity/dimension.rb', line 18

def +(other_spec)
  Dimension.new @spec.merge(other_spec.spec) {|_key,val1,val2| val1+val2}
end

#-(other_spec) ⇒ Object



21
22
23
24
# File 'lib/pulo/quantity/dimension.rb', line 21

def -(other_spec)
  new_hash = Hash[other_spec.spec.map{|k,itm| [k,-itm] } ]
  Dimension.new @spec.merge(new_hash) {|_key,val1,val2| val1+val2}
end

#/(val) ⇒ Object



28
29
30
# File 'lib/pulo/quantity/dimension.rb', line 28

def /(val)
  Dimension.new Hash[@spec.map{|itm| [itm[0],itm[1]/val]}]
end

#==(other_spec) ⇒ Object



32
33
34
35
# File 'lib/pulo/quantity/dimension.rb', line 32

def ==(other_spec)
  val=(@spec.merge(other_spec.spec) {|_key,val1,val2| val1-val2}).inject(0) {|sum,n| sum+n[1].abs}
  val==0
end

#eql?(other_spec) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/pulo/quantity/dimension.rb', line 36

def eql?(other_spec)
  self==(other_spec)
end

#hashObject



39
40
41
# File 'lib/pulo/quantity/dimension.rb', line 39

def hash
  @spec.to_s.hash
end

#specObject



10
11
12
# File 'lib/pulo/quantity/dimension.rb', line 10

def spec
  @spec
end

#to_s(supress_pretty = false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pulo/quantity/dimension.rb', line 42

def to_s(supress_pretty=false)
  sort_spec=@spec.sort_by { |item| item[1]*-1}

  if supress_pretty
    sort_spec.reduce('') do |ret,item|
      ret+item[0].to_s + item[1].to_s
    end
  else
    sort_spec.reduce('') do |ret,item|
      ret+=item[0].to_s + (item[1]==1 ? '' : Pulo.super_digit(item[1])) + '.'
    end[0..-2]
  end

end