module DuckPuncher::Ducks::Numeric

Public Instance Methods

to_currency(prefix = '') click to toggle source
# File lib/duck_puncher/ducks/numeric.rb, line 4
def to_currency(prefix = '')
  "#{prefix}%.2f" % self.round(2)
end
to_duration(seconds = false) click to toggle source
# File lib/duck_puncher/ducks/numeric.rb, line 8
def to_duration(seconds = false)
  secs = to_i
  mins = secs / 60
  hours = mins / 60
  buffer = ''
  if hours > 0
    num_mins = mins % 60
    buffer << "#{hours} h"
    buffer << " #{num_mins} min" unless num_mins.zero?
  elsif mins > 0
    num_secs = secs % 60
    buffer << "#{mins} min"
    buffer << " #{num_secs} s" if seconds && num_secs.nonzero?
  elsif seconds && secs >= 0
    buffer << "#{secs} s"
  end
  buffer
end
to_rad() click to toggle source
# File lib/duck_puncher/ducks/numeric.rb, line 46
def to_rad
  self / 180 * Math::PI
end
to_time_ago() click to toggle source

similar to Rails' time_ago_in_words

# File lib/duck_puncher/ducks/numeric.rb, line 28
def to_time_ago
  secs = to_i
  mins = secs / 60
  hours = mins / 60
  days = hours / 24
  buffer = ''
  if days > 0
    buffer << "#{days} #{'day'.pluralize(days)}"
  elsif hours > 0
    buffer << "#{hours} #{'hour'.pluralize(hours)}"
  elsif mins > 0
    buffer << "#{mins} #{'minute'.pluralize(mins)}"
  elsif secs >= 0
    buffer << "less than a minute"
  end
  buffer << ' ago'
end