aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/object/blank.rb13
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