<commit_before>#coding: utf-8
# = SIUnits
# A SI prefix unit handling library for ruby
#
# @author <NAME>
# @see https://github.com/gnomex/ruby-si-units
module SIUnits
  class Unit
    include Comparable
    attr_reader :unit_value

    # Definition of SI Prefix Units, with name, aliases and scale
    UNITS_DEFINITION = {
      'googol' => [%w{googol},            1e100],
      'yebi'   => [%w{Yi Yebi yebi},      2**80],
      'zebi'   => [%w{Zi Zebi zebi},      2**70],
      'exi'    => [%w{Ei Exi exi},        2**60],
      'pebi'   => [%w{Pi Pebi pebi},      2**50],
      'tebi'   => [%w{Ti Tebi tebi},      2**40],
      'gibi'   => [%w{Gi Gibi gibi},      2**30],
      'mebi'   => [%w{Mi Mebi mebi},      2**20],
      'kibi'   => [%w{Ki Kibi kibi},      2**10],
      'yotta'  => [%w{Y Yotta yotta},     1e24],
      'zetta'  => [%w{Z Zetta zetta},     1e21],
      'exa'    => [%w{E Exa exa},         1e18],
      'peta'   => [%w{P Peta peta},       1e15],
      'tera'   => [%w{T Tera tera},       1e12],
      'giga'   => [%w{G Giga giga},       1e9],
      'mega'   => [%w{M Mega mega},       1e6],
      'kilo'   => [%w{k kilo},            1e3],
      'hecto'  => [%w{h Hecto hecto},     1e2],
      'deca'   => [%w{da Deca deca deka}, 1e1],
      '1'      => [%w{const},                1],
      'deci'   => [%w{d Deci deci},       1e-1],
      'centi'  => [%w{c Centi centi},     1e-2],
      'milli'  => [%w{m Milli milli},     1e-3],
      'micro'  => [%w{u Micro micro},     1e-6],
      'nano'   => [%w{n Nano nano},       1e-9],
      'pico'   => [%w{p Pico pico},       1e-12],
      'femto'  => [%w{f Femto femto},     1e-15],
      'atto'   => [%w{a Atto atto},       1e-18],
      'zepto'  => [%w{z Zepto zepto},     1e-21],
      'yocto'  => [%w{y Yocto yocto},     1e-24],
      'zero'   => [%w{zero},                0.0]
    }
    UNIT_REGEX = /(\d+\.?\d*)(\w+)/

    # Create a new Unit object.
    # @return [Unit]
    # @raise [ArgumentError] if absolute value of a temperature is less than absolute zero
    # @raise [ArgumentError] if no unit is specified
    # @raise [ArgumentError] if an invalid unit is specified
    def initialize(*options)
      @unit_value  = nil #unit value
      # @base_scalar = nil #SI scale
      @unit_kind   = nil

      raise ArgumentError, "Invalid Unit Format" if options[0].nil?

      case options[0]
      when Numeric
        @unit_value = options.first
        @unit_kind = parse_unit
      when String
        value, prefix = *split_value(options[0])
        @unit_value = value.to_f
        @unit_kind = who_is_my_prefix?(prefix)
      else
        raise ArgumentError, "Invalid Unit Format"
      end
    end

    # Public call to get a unit best representation
    # @return String
    def best_scale
      @best_scale ||= best_value_with_scale
    end

    #
    def <=>(comparison)
      UNITS_DEFINITION.find_index(@unit_kind) <=> UNITS_DEFINITION.find_index(comparison.unit_kind)
    end

    # convert to a specified unit string or to the same unit as another Unit
    def convert_to(other)
      return self if other.nil?

      case other
        when Unit
          # return self if other.units == self.units
          target = other
        when String
          target = SIUnits::Unit.new(other.to_f)
        else
          raise ArgumentError, "Unknown target units"
      end
      # SIUnits::Unit.new(absolute_unit_value)
    end

    alias :>> :convert_to

    private

    # Logic to convert the absolute value to a best form of representation
    # Only aliase if scalar is zero!
    # => Don't raise a ZeroDivisionError, the zero is defined
    def best_value_with_scale
      aliase, scalar = UNITS_DEFINITION[@unit_kind]
      return aliase.first if scalar == 0
      [(@unit_value / scalar), aliase.first].join
    end

    # Logic to convert a value with scale
    # @return Float
    # all scales are defined with 1e(+|-)1, then just multiply the scalar
    def convert_base_prefix_to_value(value, scalar)
      value * scalar
    end

    # The parser
    # => Finds that the number range is contained
    # @return String with the name of representation
    # @raise ArgumentError
    def parse_unit
      case @unit_value
      when 0 then return "zero"
      when 1e-24..1e-21 then return "yocto"
      when 1e-21..1e-18 then return "atto"
      when 1e-18..1e-15 then return "femto"
      when 1e-15..1e-12 then return "pico"
      when 1e-12..1e-9 then return "nano"
      when 1e-9..1e-6 then return "micro"
      when 1e-6..1e-3 then return "milli"
      when 1e-3..1e-2 then return "centi"
      when 1e-2..1e-1 then return "deci"<commit_msg>Add to_s method, round value to two decimal places<commit_after>#coding: utf-8
# = SIUnits
# A SI prefix unit handling library for ruby
#
# @author <NAME>
# @see https://github.com/gnomex/ruby-si-units
module SIUnits
  class Unit
    include Comparable
    attr_reader :unit_value

    # Definition of SI Prefix Units, with name, aliases and scale
    UNITS_DEFINITION = {
      'googol' => [%w{googol},            1e100],
      'yebi'   => [%w{Yi Yebi yebi},      2**80],
      'zebi'   => [%w{Zi Zebi zebi},      2**70],
      'exi'    => [%w{Ei Exi exi},        2**60],
      'pebi'   => [%w{Pi Pebi pebi},      2**50],
      'tebi'   => [%w{Ti Tebi tebi},      2**40],
      'gibi'   => [%w{Gi Gibi gibi},      2**30],
      'mebi'   => [%w{Mi Mebi mebi},      2**20],
      'kibi'   => [%w{Ki Kibi kibi},      2**10],
      'yotta'  => [%w{Y Yotta yotta},     1e24],
      'zetta'  => [%w{Z Zetta zetta},     1e21],
      'exa'    => [%w{E Exa exa},         1e18],
      'peta'   => [%w{P Peta peta},       1e15],
      'tera'   => [%w{T Tera tera},       1e12],
      'giga'   => [%w{G Giga giga},       1e9],
      'mega'   => [%w{M Mega mega},       1e6],
      'kilo'   => [%w{k kilo},            1e3],
      'hecto'  => [%w{h Hecto hecto},     1e2],
      'deca'   => [%w{da Deca deca deka}, 1e1],
      '1'      => [%w{const},                1],
      'deci'   => [%w{d Deci deci},       1e-1],
      'centi'  => [%w{c Centi centi},     1e-2],
      'milli'  => [%w{m Milli milli},     1e-3],
      'micro'  => [%w{u Micro micro},     1e-6],
      'nano'   => [%w{n Nano nano},       1e-9],
      'pico'   => [%w{p Pico pico},       1e-12],
      'femto'  => [%w{f Femto femto},     1e-15],
      'atto'   => [%w{a Atto atto},       1e-18],
      'zepto'  => [%w{z Zepto zepto},     1e-21],
      'yocto'  => [%w{y Yocto yocto},     1e-24],
      'zero'   => [%w{zero},                0.0]
    }
    UNIT_REGEX = /(\d+\.?\d*)(\w+)/

    # Create a new Unit object.
    # @return [Unit]
    # @raise [ArgumentError] if absolute value of a temperature is less than absolute zero
    # @raise [ArgumentError] if no unit is specified
    # @raise [ArgumentError] if an invalid unit is specified
    def initialize(*options)
      @unit_value  = nil #unit value
      # @base_scalar = nil #SI scale
      @unit_kind   = nil

      raise ArgumentError, "Invalid Unit Format" if options[0].nil?

      case options[0]
      when Numeric
        @unit_value = options.first
        @unit_kind = parse_unit
      when String
        value, prefix = *split_value(options[0])
        @unit_value = value.to_f
        @unit_kind = who_is_my_prefix?(prefix)
      else
        raise ArgumentError, "Invalid Unit Format"
      end
    end

    # Public call to get a unit best representation
    # @return String
    def best_scale
      @best_scale ||= best_value_with_scale
    end

    #
    def <=>(comparison)
      UNITS_DEFINITION.find_index(@unit_kind) <=> UNITS_DEFINITION.find_index(comparison.unit_kind)
    end

    # convert to a specified unit string or to the same unit as another Unit
    def convert_to(other)
      return self if other.nil?

      case other
        when Unit
          # return self if other.units == self.units
          target = other
        when String
          target = SIUnits::Unit.new(other.to_f)
        else
          raise ArgumentError, "Unknown target units"
      end
      # SIUnits::Unit.new(absolute_unit_value)
    end

    alias :>> :convert_to

    def to_s
      "#{"%.2f" % unit_value } #{ @unit_kind }"
    end

    private

    # Logic to convert the absolute value to a best form of representation
    # Only aliase if scalar is zero!
    # => Don't raise a ZeroDivisionError, the zero is defined
    def best_value_with_scale
      aliase, scalar = UNITS_DEFINITION[@unit_kind]
      return aliase.first if scalar == 0
      [(@unit_value * scalar), aliase.first].join
    end

    # Logic to convert a value with scale
    # @return Float
    # all scales are defined with 1e(+|-)1, then just multiply the scalar
    def convert_base_prefix_to_value(value, scalar)
      value * scalar
    end

    # The parser
    # => Finds that the number range is contained
    # @return String with the name of representation
    # @raise ArgumentError
    def parse_unit
      case @unit_value
      when 0 then return "zero"
      when 1e-24..1e-21 then return "yocto"
      when 1e-21..1e-18 then return "atto"
      when 1e-18..1e-15 then return "femto"
      when 1e-15..1e-12 then return "pico"
      when 1e-12..1e-9 then return "nano"
      when 1e-9..1e-6 then return "micro"
      when 1e-6..1e-3 then return "milli"
      when 1e-3..1e-2 then return "centi"
      when 1e-2..1e-1 then return "deci"
{'fraction_non_alphanumeric': 0.13654234406545532, 'fraction_numerical': 0.028796089682286686, 'mean_word_length': 2.042016806722689, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 79, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6303251', 'n_tokens_mistral': 3546, 'n_tokens_neox': 3371, 'n_words': 951}