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.
1 minute read
Rails 6 has addedindex_with to Enumerable.
This allows creating a hash from an enumerable with the value from a passed block or a default argument.
In one of our applications,
we need to perform some params filtering,
after which we pass it to ElasticSearch queries.
We receive unfiltered terms from Searchkit which
are then filtered as below by sanitize_terms method.
As you can see the code in sanitized_terms gets verbose,
since we first initialize the hash,
mutate it, and then return its value.
This kind of task is not rare,
we need to perform it at various places.
It provides us with a convenience method to perform this operation in a simplified form.
index_with creates a new hash object,
using the key as the current element it is enumerating.
Value is determined from the output of the passed block.
This helps us simplify our initial example in a much easier way:
index_with also allows to create a hash easily with default values: