Module: RubyUnits::String

Included in:
String
Defined in:
lib/ruby_units/string.rb

Overview

Extra methods for converting [String] objects to [RubyUnits::Unit] objects and using string formatting with Units.

Instance Method Summary collapse

Instance Method Details

#%(*other) ⇒ String

Format unit output using formatting codes

Examples:

‘%0.2f’ % ‘1 mm’.to_unit => ‘1.00 mm’

Parameters:

Returns:



22
23
24
25
26
27
28
# File 'lib/ruby_units/string.rb', line 22

def %(*other)
  if other.first.is_a?(RubyUnits::Unit)
    other.first.to_s(self)
  else
    super
  end
end

#convert_to(other) ⇒ Unit

Parameters:

Returns:



32
33
34
# File 'lib/ruby_units/string.rb', line 32

def convert_to(other)
  to_unit.convert_to(other)
end

#to_unit(other = nil) ⇒ RubyUnits::Unit

Make a string into a unit

Parameters:

Returns:



13
14
15
# File 'lib/ruby_units/string.rb', line 13

def to_unit(other = nil)
  other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
end