Module: DuckPuncher::Ducks::Numeric
- Defined in:
- lib/duck_puncher/ducks/numeric.rb
Instance Method Summary collapse
- #to_currency(prefix = '') ⇒ Object
- #to_duration(seconds = false) ⇒ Object
- #to_rad ⇒ Object
-
#to_time_ago ⇒ Object
similar to Rails’ #time_ago_in_words.
Instance Method Details
#to_currency(prefix = '') ⇒ Object
4 5 6 |
# File 'lib/duck_puncher/ducks/numeric.rb', line 4 def to_currency(prefix = '') "#{prefix}%.2f" % self.round(2) end |
#to_duration(seconds = false) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 ⇒ Object
46 47 48 |
# File 'lib/duck_puncher/ducks/numeric.rb', line 46 def to_rad self / 180 * Math::PI end |
#to_time_ago ⇒ Object
similar to Rails’ #time_ago_in_words
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# 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 |