instance method
returning
Ruby on Rails 2.3.18
Last seen in v3.0.20Signature
returning(value)
Returns value after yielding value to the block. This simplifies the process of constructing an object, performing work on the object, and then returning the object from a method. It is a Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
Examples
# Without returning
def foo
values = []
values << "bar"
values << "baz"
return values
end
foo # => ['bar', 'baz']
# returning with a local variable
def foo
returning values = [] do
values << 'bar'
values << 'baz'
end
end
foo # => ['bar', 'baz']
# returning with a block argument
def foo
returning [] do |values|
values << 'bar'
values << 'baz'
end
end
foo # => ['bar', 'baz']
Parameters
-
valuereq
Source
# File activesupport/lib/active_support/core_ext/object/misc.rb, line 40
def returning(value)
ActiveSupport::Deprecation.warn('Kernel#returning has been deprecated in favor of Object#tap.', caller)
yield(value)
value
end
Defined in activesupport/lib/active_support/core_ext/object/misc.rb line 40
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in Object