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

Instance Method Details

#count_values(values) ⇒ Hash{Object, Integer} #count_values(values) { ... } ⇒ Hash{Object, Integer}

Overloads:

  • #count_values(values) ⇒ Hash{Object, Integer}

    Counts the number of times each value appears in the enumerable object.

    Examples:

    ArrayTools.count_values([1, 1, 1, 2, 2, 3])
    #=> { 1 => 3, 2 => 2, 3 => 1 }
    

    Parameters:

    • values (Array<Object>)

      The values to count.

    Returns:

    • (Hash{Object, Integer})

      The number of times each value appears in the enumerable object.

  • #count_values(values) { ... } ⇒ Hash{Object, Integer}

    Calls the block with each item and counts the number of times each result appears.

    Examples:

    ArrayTools.count_values([1, 1, 1, 2, 2, 3]) { |i| i ** 2 }
    #=> { 1 => 3, 4 => 2, 9 => 1 }
    

    Parameters:

    • values (Array<Object>)

      The values to count.

    Yields:

    • item An item in the array to be converted to a countable result.

    Returns:

    • (Hash{Object, Integer})

      The number of times each result appears.



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.

Examples:

With Zero Items

ArrayTools.humanize_list([])
#=> ''

With One Item

ArrayTools.humanize_list(['spam'])
#=> 'spam'

With Two Items

ArrayTools.humanize_list(['spam', 'eggs'])
#=> 'spam and eggs'

With Three Or More Items

ArrayTools.humanize_list(['spam', 'eggs', 'bacon', 'spam'])
#=> 'spam, eggs, bacon, and spam'

With Three Or More Items And Options

ArrayTools.humanize_list(['spam', 'eggs', 'bacon', 'spam'], :last_separator => ' or ')
#=> 'spam, eggs, bacon, or spam'

Parameters:

  • values (Array<String>)

    The list of values to format. Will be coerced to strings using #to_s.

  • options (Hash) (defaults to: {})

    Optional configuration hash.

Options Hash (options):

  • :last_separator (String)

    The value to use to separate the final pair of values. Defaults to “ and ” (note the leading and trailing spaces). Will be combined with the :separator for lists of length 3 or greater.

  • :separator (String)

    The value to use to separate pairs of values before the last in lists of length 3 or greater. Defaults to “, ” (note the trailing space).

Returns:

  • (String)

    The formatted string.



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, options = {}
  separator = options.fetch(:separator, ', ')
  last_separator = options.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