Exploring Ruby-2.7.0 — Part-Three
Published in
1 min readJun 10, 2019
Ruby-2.7 adds Enumerable#filter_map
ruby-2.7 adds filter_map which combines filter and map in one iteration.
Let us suppose you have a collection of employee records. Assume you have to get the employee_id’s of all contract employees. This could be achieved using filter_map
Before ruby.27.0
Why you must use filter_map?
- There would be a significant speedup as filter_map performs the operation in a single iteration
- By using filter_map your code looks more elegant to read
- filter_map could come very handy when manipulating Active Record object collections
I am adding more examples from the test cases
The only caveat is it would be impossible to intentionally return nil.
if you are interested how filter_map is implemented check this github link