Sha256: 22fe425a52368bdf64f217c6a652a1dec5a7b0cd31bf5eacfe79838826c4c610

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

class Numeric

  def clamp(min, max)
    self < min ? min : (self > max ? max : self)
  end

  def lerp(target_value, percent)
    self + (target_value - self) * percent.clamp(0, 1.0)
  end

  def to_period(detailed = true, abbreviated = true)
    total_seconds = self.to_i

    days = total_seconds / 86400
    hours = (total_seconds / 3600) - (days * 24)
    minutes = (total_seconds / 60) - (hours * 60) - (days * 1440)
    seconds = total_seconds % 60

    elements = []
    if days > 0
      suffix = abbreviated ? 'd' : (days == 1 ? ' day' : ' days')
      elements << "#{days}#{suffix}"
    end
    if hours > 0
      suffix = abbreviated ? 'h' : (hours == 1 ? ' hour' : ' hours')
      elements << "#{hours}#{suffix}"
    end
    if minutes > 0 and (detailed or days == 0)
      suffix = abbreviated ? 'm' : (minutes == 1 ? ' minute' : ' minutes')
      elements << "#{minutes}#{suffix}"
    end
    if seconds > 0 and (detailed or (days == 0 and hours == 0))
      suffix = abbreviated ? 's' : (seconds == 1 ? ' second' : ' seconds')
      elements << "#{seconds}#{suffix}"
    end

    elements.join(abbreviated ? ' ' : ', ')
  end

  def to_formatted_time
    return '00:00' if self == 0

    minutes = (self / 60).floor
    seconds = (self % 60).round
    minutes = '0' + minutes.to_s if minutes.to_s.length == 1
    seconds = '0' + seconds.to_s if seconds.to_s.length == 1
    "#{minutes}:#{seconds}"
  end
end

class Range
  def random
    rand * (max - min) + min
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bootstripe-0.2.23 lib/bootstripe/number_additions.rb
bootstripe-0.2.22 lib/bootstripe/number_additions.rb
bootstripe-0.2.20 lib/bootstripe/number_additions.rb
bootstripe-0.2.19 lib/bootstripe/number_additions.rb
bootstripe-0.2.18 lib/bootstripe/number_additions.rb
bootstripe-0.2.17 lib/bootstripe/number_additions.rb