aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2014-08-29 14:58:36 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2014-08-29 14:58:36 -0700
commit141d864e0ec1098176141e6cb6aef3eaf07e830f (patch)
tree230f6a02b842585c354df231d03fee6975d7dca7 /activesupport/lib/active_support/core_ext
parent7475b43cdbbbf7456e798210cb97ef20f2225166 (diff)
downloadrails-141d864e0ec1098176141e6cb6aef3eaf07e830f.tar.gz
rails-141d864e0ec1098176141e6cb6aef3eaf07e830f.tar.bz2
rails-141d864e0ec1098176141e6cb6aef3eaf07e830f.zip
Added yield to Object#presence
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