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:



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

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

#convert_to(other) ⇒ Unit

Parameters:

Returns:



30
31
32
# File 'lib/ruby_units/string.rb', line 30

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

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

Make a string into a unit

Parameters:

Returns:



11
12
13
# File 'lib/ruby_units/string.rb', line 11

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