I am an active member of Ruby community. I have been consistently contributing to Ruby on Rails for a number of years and now am one of the top 30 contributors to Ruby on Rails. I also help as co-editor for the This week in Rails newsletter. Besides Ruby on Rails I have also contributed to many other notable open source projects including Sinatra, Devise and Rake. I am a seasoned speaker an have spoken at many conferences around the world including Gogaruco in San Francisco, RedDotRubyConf in Singapore, RubyConfIndia in Goa, India MadisonPlusRuby in Madison, Wisconsin, RubyConfBrazil in Suo Paulo, Brazil, and RubyConf Philippines in Manilla, Philippines. I am organizer of Deccan Ruby Conference and used to run RubyIndia Podcast. During my early days of open source as part of "Google summer of code" I contributed to the krypt-project project. Later I helped mentor in the JRuby and currently mentor in the Ruby on Rails organization for Google summer of code. When not working on Ruby, I am mostly working on Reactjs. I have authored the book Building Modern Web Applications with React.js which is published by PACKT. I have produced a number of screencasts on the topic of Learn React.js.
filter_map is a shorthand for filter + map in a single call.
Before
Originally suggested 7 years ago,
it was addressing a common idiom
to map over an Enumerable,
but only include the elements
that match a particular filter
Another common approach is selecting + mapping,
which is pretty common, for example in ActiveRecord,
when manipulating loaded records:
It’s also possible to achieve something similar with:
Enumerable#filter_map
We can now use Enumerable#filter_map in Ruby 2.7 to achieve this:
As you see, filter_map creates a new array
after first filtering desired results,
and then maps to get expected Array.
This comes in pretty handy for creating mapped
arrays in a simpler way.
It also gives us a bit of a speedup from its previous counter-parts
As you can see, this is faster with about 10-20% speedup.