Scott Watermasysk Husband, Father, and KickoffLabs co-founder. Interests: basketball, bootstrapping, keyboards, training, and Building new things

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 = ? : 'N.A.'

This would be much simpler with presence

name = || 'N.A.'

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