Skip to content

Latest commit

 

History

History
31 lines (20 loc) · 906 Bytes

conditionals_when_object_is_nil.md

File metadata and controls

31 lines (20 loc) · 906 Bytes

Conditionals when object is nil

Before:

event.end_date.nil? ? '' : event.end_date.to_s(:long)

After:

event.end_date.try(:to_s, :long)

Or (2.3+):

event.end_date&.to_s&.long

About &. operator

Remembering: try is a Rails method and it's not in Ruby Core.

The documentation for try says:

Invokes the method identified by the symbol method, passing it any arguments and/or the block specified, just like Ruby. Object#send. Unlike that method, nil will be returned if the receiving object is a nil object or NilClass.

It’s usually worth spending time identifying why you have to do this at all. If you can remove the scenario where the object is nil, that’s preferable.

From: https://robots.thoughtbot.com/code-review-ruby-and-rails-idioms