Sha256: 5b49c4c007e9ae3152cb2ca3e9acbb904b61aa5e5edd819d744700a86c19cbaa

Contents?: true

Size: 731 Bytes

Versions: 2

Compression:

Stored size: 731 Bytes

Contents

require 'time'
class String
  # make a string into a unit
  # @return (see RubyUnits::Unit#initialize)
  def to_unit(other = nil)
    other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
  end

  alias_method :original_format, :%
  # format unit output using formating codes
  # @example '%0.2f' % '1 mm'.to_unit => '1.00 mm'
  # @return [String]
  def format_with_unit(*other)
    case
    when other.first.is_a?(RubyUnits::Unit)
      other.first.to_s(self)
    else
      original_format(*other)
    end
  end
  alias_method :%, :format_with_unit

  # @param (see RubyUnits::Unit#convert_to)
  # @return (see RubyUnits::Unit#convert_to)
  def convert_to(other)
    to_unit.convert_to(other)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-units-2.0.1 lib/ruby_units/string.rb
ruby-units-2.0.0 lib/ruby_units/string.rb