File tree 5 files changed +20
-34
lines changed
lib/active_support/core_ext/object
5 files changed +20
-34
lines changed Original file line number Diff line number Diff line change 1
- * Added yield to Object#presence , so you can do this:
1
+ * Added instance_eval version to Object#try , so you can do this:
2
2
3
- project.account.owner.presence { name.first } || 'Nobody'
3
+ person.try { name.first }
4
4
5
- instead of calling twice (which may incur double SQL calls) :
5
+ instead of:
6
6
7
- project.account.owner ? project.account.owner.name.first || 'Nobody'
8
-
9
- or assigning to local variable:
10
-
11
- owner = project.account.owner
12
- owner ? owner.name.first || 'Nobody'
7
+ person.try { |person| person.name.first }
13
8
14
9
* DHH*
15
10
Original file line number Diff line number Diff line change @@ -39,21 +39,9 @@ def present?
39
39
#
40
40
# region = params[:state].presence || params[:country].presence || 'US'
41
41
#
42
- # You can also use this with a block that will be yielded if the object is present
43
- # and the result of that block will then be returned. The block itself is run against
44
- # the instance you're running #presence on (using instance_eval)
45
- #
46
- # project.account.owner.presence { name.first } || 'Nobody'
47
- #
48
42
# @return [Object]
49
- def presence ( &block )
50
- if present?
51
- if block_given?
52
- instance_eval ( &block )
53
- else
54
- self
55
- end
56
- end
43
+ def presence
44
+ self if present?
57
45
end
58
46
end
59
47
Original file line number Diff line number Diff line change @@ -33,14 +33,23 @@ class Object
33
33
# ...
34
34
# end
35
35
#
36
+ # You can also call try with a block without accepting an argument, and the block
37
+ # will be instance_eval'ed instead:
38
+ #
39
+ # @person.try { upcase.truncate(50) }
40
+ #
36
41
# Please also note that +try+ is defined on +Object+, therefore it won't work
37
42
# with instances of classes that do not have +Object+ among their ancestors,
38
43
# like direct subclasses of +BasicObject+. For example, using +try+ with
39
44
# +SimpleDelegator+ will delegate +try+ to the target instead of calling it on
40
45
# delegator itself.
41
46
def try ( *a , &b )
42
47
if a . empty? && block_given?
43
- yield self
48
+ if b . arity . zero?
49
+ instance_eval ( &b )
50
+ else
51
+ yield self
52
+ end
44
53
else
45
54
public_send ( *a , &b ) if respond_to? ( a . first )
46
55
end
Original file line number Diff line number Diff line change @@ -28,14 +28,4 @@ def test_present
28
28
BLANK . each { |v | assert_equal false , v . present? , "#{ v . inspect } should not be present" }
29
29
NOT . each { |v | assert_equal true , v . present? , "#{ v . inspect } should be present" }
30
30
end
31
-
32
- def test_presence
33
- BLANK . each { |v | assert_equal nil , v . presence , "#{ v . inspect } .presence should return nil" }
34
- NOT . each { |v | assert_equal v , v . presence , "#{ v . inspect } .presence should return self" }
35
- end
36
-
37
- def test_presence_with_a_block
38
- assert_equal "THIS WAS TENDERLOVE'S IDEA" , "this was tenderlove's idea" . presence { upcase } || "Nobody"
39
- assert_equal "Nobody" , nil . presence { upcase } || "Nobody"
40
- end
41
31
end
Original file line number Diff line number Diff line change @@ -65,6 +65,10 @@ def test_try_only_block_nil
65
65
assert_equal false , ran
66
66
end
67
67
68
+ def test_try_with_instance_eval_block
69
+ assert_equal @string . reverse , @string . try { reverse }
70
+ end
71
+
68
72
def test_try_with_private_method_bang
69
73
klass = Class . new do
70
74
private
You can’t perform that action at this time.
0 commit comments