Method: Array#time_string
- Defined in:
- lib/doing/array_chronify.rb
#time_string(format: :dhm) ⇒ String
Format [d, h, m] as string
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/doing/array_chronify.rb', line 14 def time_string(format: :dhm) raise InvalidArgument, 'Invalid array, must be [d,h,m]' unless count == 3 d, h, m = self case format when :clock format('%<d>02d:%<h>02d:%<m>02d', d: d, h: h, m: m) when :dhm output = [] output.push(format('%<d>dd', d: d)) if d.positive? output.push(format('%<h>dh', h: h)) if h.positive? output.push(format('%<m>dm', m: m)) if m.positive? output.join(' ') when :hm h += d * 24 if d.positive? format('%<h> 4dh %<m>02dm', h: h, m: m) when :m h += d * 24 if d.positive? m += h * 60 if h.positive? format('%<m> 4dm', m: m) when :natural human = [] human.push(format('%<d>d %<s>s', d: d, s: 'day'.to_p(d))) if d.positive? human.push(format('%<h>d %<s>s', h: h, s: 'hour'.to_p(h))) if h.positive? human.push(format('%<m>d %<s>s', m: m, s: 'minute'.to_p(m))) if m.positive? human.join(', ') when :speech human = [] human.push(format('%<d>d %<s>s', d: d, s: 'day'.to_p(d))) if d.positive? human.push(format('%<h>d %<s>s', h: h, s: 'hour'.to_p(h))) if h.positive? human.push(format('%<m>d %<s>s', m: m, s: 'minute'.to_p(m))) if m.positive? last = human.pop case human.count when 2 human.join(', ') + ", and #{last}" when 1 "#{human[0]} and #{last}" when 0 last end end end |