New methods introduced in Ruby 2.7

Ruby 2.7 introduced some helpful methods:


Enumerable#tally allows to count the same elements in the array:

%i[foo bar foo bar bar baz].tally
=> {:foo=>2, :bar=>3, :baz=>1}

Enumerable#tally_by is still under consideration.


Enumerator#filter_map simply combines filter and map in a single function:

(1..8).filter_map { |n| n ** 2 if n.even? }
=> [4, 16, 36, 64]


Enumerator#produce generates an enumerator where next element is calculated by block where previous element is passed as an argument.

sequence = Enumerator.produce(1) { |n| n * 2 }
=> [1, 2, 4]


Array#intersection is more of an alias (to Array#&) rather than a new functionality:

%i[foo bar].intersection(%i[foo])
=> [:foo]

Numbered parameters for blocks

Not a method but cool feature allowing to access to block arguments based on their position:

%i[foo bar].map { _1 }
=> [:foo, :bar]
%i[foo bar] { "#{_1}=#{_2}" }
=> ["foo=0", "bar=1"]