Class: Pulo::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/pulo/quantity/unit.rb

Direct Known Subclasses

NonSI_Unit, SI_Unit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, plural, abbreviation) ⇒ Unit

Returns a new instance of Unit.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/pulo/quantity/unit.rb', line 6

def initialize(name, plural, abbreviation)
  case plural
    when :s
      plural=name.to_s + 's'
    when :sf
      plural=name.to_s.sub(/(?<=.)_{1}/, 's_')
    else
      plural=plural.to_s
  end
  @name, @plural, @abbreviation=name, plural, abbreviation
end

Instance Attribute Details

#abbreviationObject (readonly)

Returns the value of attribute abbreviation.



5
6
7
# File 'lib/pulo/quantity/unit.rb', line 5

def abbreviation
  @abbreviation
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/pulo/quantity/unit.rb', line 5

def name
  @name
end

#pluralObject (readonly)

Returns the value of attribute plural.



5
6
7
# File 'lib/pulo/quantity/unit.rb', line 5

def plural
  @plural
end

Instance Method Details

#to_sObject



17
18
19
20
21
22
23
24
25
# File 'lib/pulo/quantity/unit.rb', line 17

def to_s
  ret=abbreviation.ljust(12,' ') + name.to_s.ljust(30) + plural.ljust(30)
  if self.is_si?
    ret+='*10^' + self.scale.round(2).to_s.ljust(7,' ')
  else
    ret+=self.si_convert_factor.round(4).to_s + ' per ' + self.si_convert_unit.to_s
  end
  ret
end