Object#presence is new to object/blank.rb in ActiveSupport (View source). It simply returns self if self.present? is true, otherwise it returns nil. This works great for blank strings and empty arrays:
Let's give our blog posts a default title if a blank one is provided:
Now let's refactor it with Object#presence:
How cute.
0 comments:
Post a Comment