Scott Watermasysk Husband, Father, Bootstrapper, Developer, Builder, Shipper.

Active Support - Presence

Active Support as a whole gets a bad wrap. Ruby is all about developer happiness and that is really what Active Support is all about.

It offers a richer bottom-line at the language level, targeted both at the development of Rails applications, and at the development of Ruby on Rails itself.

One small extension I love is presence.

In short, it returns itself if .present? is true. Otherwise, it returns nil

1.presence  # => 1 
"Hello World".presence # => Hello World
"".presence # => nil

Unfortunately, I have seen much more usage of .present? directly.

For example:

name = user.name.present? ? user.name : 'N.A.'

This would be much simpler with presence

name = user.name.presence || 'N.A.'

If you are not familiar with Ruby, you may be asking why not just use user.name || 'N.A.' directly. The present? (and related blank?) extension will handle empty strings which in Ruby are evaluated as truthy.