From 141d864e0ec1098176141e6cb6aef3eaf07e830f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 29 Aug 2014 14:58:36 -0700 Subject: Added yield to Object#presence --- activesupport/lib/active_support/core_ext/object/blank.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/core_ext') 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 -- cgit v1.2.3