inspiration of experience

I'm going to write some experience of my life, art and work, but sometimes I don't feel like sharing it with someone, it depends on how I feel right now..

April 27, 2011 at 8:49pm
Home
Reblogged from rubyquicktips

Using “try” with a hash to check existence of a key

rubyquicktips:

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.

(Source: rubyquicktips)

Notes

  1. nopolitica reblogged this from rubyquicktips
  2. samshull reblogged this from rubyquicktips
  3. rubyquicktips posted this