diff options
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/blank.rb | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 38e43478df..164a3c47d0 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -39,9 +39,20 @@ class Object # # region = params[:state].presence || params[:country].presence || 'US' # + # You can also use this with a block that will be yielded if the object is present + # and the result of that block will then be returned + # + # person.presence { |p| p.name.first } || 'Nobody' + # # @return [Object] def presence - self if present? + if present? + if block_given? + yield self + else + self + end + end end end |