Skip to content

Commit 88b5f93

Browse files
committed
Bring returning back to ease migration.
1 parent 9effe3c commit 88b5f93

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

activesupport/lib/active_support/core_ext/object.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'active_support/core_ext/object/blank'
33
require 'active_support/core_ext/object/duplicable'
44
require 'active_support/core_ext/object/try'
5+
require 'active_support/core_ext/object/returning'
56

67
require 'active_support/core_ext/object/conversions'
78
require 'active_support/core_ext/object/instance_variables'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Object
2+
# Returns +value+ after yielding +value+ to the block. This simplifies the
3+
# process of constructing an object, performing work on the object, and then
4+
# returning the object from a method. It is a Ruby-ized realization of the K
5+
# combinator, courtesy of Mikael Brockman.
6+
#
7+
# ==== Examples
8+
#
9+
# # Without returning
10+
# def foo
11+
# values = []
12+
# values << "bar"
13+
# values << "baz"
14+
# return values
15+
# end
16+
#
17+
# foo # => ['bar', 'baz']
18+
#
19+
# # returning with a local variable
20+
# def foo
21+
# returning values = [] do
22+
# values << 'bar'
23+
# values << 'baz'
24+
# end
25+
# end
26+
#
27+
# foo # => ['bar', 'baz']
28+
#
29+
# # returning with a block argument
30+
# def foo
31+
# returning [] do |values|
32+
# values << 'bar'
33+
# values << 'baz'
34+
# end
35+
# end
36+
#
37+
# foo # => ['bar', 'baz']
38+
def returning(value)
39+
ActiveSupport::Deprecation.warn('Object#returning has been deprecated in favor of Object#tap.', caller)
40+
yield(value)
41+
value
42+
end
43+
end

0 commit comments

Comments
 (0)