Sha256: 3bab1e9d3b707cca59b294caa4e23774cf8c64ef7538288d237d2171df571c31

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

class Numeric
  include ErrorHelper

  Alpha26 = ("a".."z").to_a

  def to_s26
    return "" if self < 1

    s, q = "", self
    loop do
      q, r = (q - 1).divmod(26)
      s.prepend(Alpha26[r])
      break if q.zero?
    end
    s
  end

  def safe_part(part)
    if self > 0
      if part >= 0
        return part.to_f/self.to_f
      else
        raise_positive_number_only
      end
    elsif self == 0
      return 0
    else
      raise_positive_number_only
    end
  end

  def safe_percent(part, precision: 15)
    if self > 0
      if part >= 0
        return (safe_part(part)*100).round(precision)
      else
        raise_positive_number_only
      end
    elsif self == 0
      return 0
    else
      raise_positive_number_only
    end
  end

  def safe_per(parts)
    return 0 if self == 0 || parts == 0

    return self.to_f/parts.to_f
  end

  def to_time_hash
    mm, ss = self.divmod(60)
    hh, mm = mm.divmod(60)
    dd, hh = hh.divmod(24)

    return {
      days: dd,
      hours: hh,
      minutes: mm,
      seconds: ss,
    }
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
creative_rails_utilities-0.4.8 lib/creative_rails_utilities/numeric.rb
creative_rails_utilities-0.4.7 lib/creative_rails_utilities/numeric.rb
creative_rails_utilities-0.4.6 lib/creative_rails_utilities/numeric.rb
creative_rails_utilities-0.4.5 lib/creative_rails_utilities/numeric.rb
creative_rails_utilities-0.4.4 lib/creative_rails_utilities/numeric.rb
creative_rails_utilities-0.4.3 lib/creative_rails_utilities/numeric.rb
creative_rails_utilities-0.4.2 lib/creative_rails_utilities/numeric.rb
creative_rails_utilities-0.4.1 lib/creative_rails_utilities/numeric.rb