Module: SleepingKingStudios::Tools::EnumerableTools
- Extended by:
- EnumerableTools
- Included in:
- EnumerableTools
- Defined in:
- lib/sleeping_king_studios/tools/enumerable_tools.rb
Overview
Tools for working with enumerable objects, such as arrays and hashes.
Instance Method Summary collapse
- #count_values(values, &block) ⇒ Object
-
#humanize_list(values, options = {}) ⇒ String
Accepts a list of values and returns a human-readable string of the values, with the format based on the number of items.
Instance Method Details
#count_values(values) ⇒ Hash{Object, Integer} #count_values(values) { ... } ⇒ Hash{Object, Integer}
36 37 38 39 40 41 |
# File 'lib/sleeping_king_studios/tools/enumerable_tools.rb', line 36 def count_values values, &block values.each.with_object({}) do |item, hsh| value = block_given? ? block.call(item) : item hsh[value] = hsh.fetch(value, 0) + 1 end # each end |
#humanize_list(values, options = {}) ⇒ String
Accepts a list of values and returns a human-readable string of the values, with the format based on the number of items.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/sleeping_king_studios/tools/enumerable_tools.rb', line 78 def humanize_list values, = {} separator = .fetch(:separator, ', ') last_separator = .fetch(:last_separator, ' and ') case values.count when 0 '' when 1 values.first.to_s when 2 "#{values[0]}#{last_separator}#{values[1]}" else if last_separator =~ /\A,?\s*/ last_separator = last_separator.sub /\A,?\s*/, separator else last_separator = "#{separator}#{last_separator}" end # if-else "#{values[0...-1].join(separator)}#{last_separator}#{values.last}" end # case end |