The try method is awesome. Check the documentation.
It is usually used to call a method on an object if it exists, or return nil if it doesn’t.
But sometimes, it is not used with hashes, but this also works perfectly:
params[:search] ? params[:search][:name] : nil # Can also be written as... params[:search].try(:[],:name)
Clean!
This tip was submitted by Miguel Camba.
deepcleaningcorona reblogged this from rubyquicktips You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.