Skip to content
instance method

Enumerable#tally

(?::Hash[Elem, ::Integer] hash) -> ::Hash[Elem, ::Integer]
tally(hash = {}) → hash

When argument hash is not given, returns a new hash whose keys are the distinct elements in self; each integer value is the count of occurrences of each element:

Example
%w[a b c b c a c b].tally # => {"a"=>2, "b"=>3, "c"=>3}

When argument hash is given, returns hash, possibly augmented; for each element ele in self:

  • Adds it as a key with a zero value if that key does not already exist:

    Example
    hash[ele] = 0 unless hash.include?(ele)
    
  • Increments the value of key ele:

    Example
    hash[ele] += 1
    

This is useful for accumulating tallies across multiple enumerables:

Example
h = {}                   # => {}
%w[a c d b c a].tally(h) # => {"a"=>2, "c"=>2, "d"=>1, "b"=>1}
%w[b a z].tally(h)       # => {"a"=>3, "c"=>2, "d"=>1, "b"=>2, "z"=>1}
%w[b a m].tally(h)       # => {"a"=>4, "c"=>2, "d"=>1, "b"=>3, "z"=>1, "m"=>1}

The key to be added or found for an element depends on the class of self; see Enumerable in Ruby Classes.

Examples:

  • Array (and certain array-like classes): the key is the element (as above).

  • Hash (and certain hash-like classes): the key is the 2-element array formed from the key-value pair:

    Example
    h = {}                        # => {}
    {foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1}
    {foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1, [:foo, "c"]=>1, [:bar, "d"]=>1}
    {foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>1, [:bar, "d"]=>1}
    {foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>2, [:bar, "d"]=>2}
    

Ruby version changes

  1. 3.1
See also #take_while #to_a #take #to_h also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0