class Array
Array
helpers
Array
helpers
Chronify array helpers
Public Instance Methods
good?()
click to toggle source
Tests if object is nil or empty
@return [Boolean] true if object is defined and has content
# File lib/doing/good.rb, line 36 def good? !nil? && !empty? end
nested_hash(value = nil)
click to toggle source
Convert array to nested hash, setting last key to value
@param value The value to set
# File lib/doing/array/nested_hash.rb, line 11 def nested_hash(value = nil) hsh = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) } hsh.dig(*self[0..-2])[fetch(-1)] = value hsh end
time_string(format: :dhm)
click to toggle source
Format [d, h, m] as string
@accept [Array] Array
of [days, hours, minutes]
@param format [Symbol] The format, :dhm, :hm, :m, :clock, :natural @return [String] formatted string
# File lib/doing/chronify/array.rb, line 15 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 if d.zero? && h > 24 d = (h / 24).floor h = h % 24 end format('%<d>02d:%<h>02d:%<m>02d', d: d, h: h, m: m) when :dhm if d.zero? && h > 24 d = (h / 24).floor h = h % 24 end 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
utf8()
click to toggle source
Force UTF-8 encoding of strings in array
@return [Array] Encoded lines
# File lib/doing/array/array.rb, line 12 def utf8 c = self.class if String.method_defined? :force_encoding replace c.new(map(&:utf8)) else self end end