diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-05-20 16:49:21 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-05-20 18:12:44 -0700 |
commit | 68398838544a778244e6028f2c30deb7313ea0b2 (patch) | |
tree | d37102f36827549733ac8f241fd965003d7eedd8 /activesupport/lib/active_support/core_ext/object/returning.rb | |
parent | 429a00f225b24b938575ed40d11fa07987e12a6f (diff) | |
download | rails-68398838544a778244e6028f2c30deb7313ea0b2.tar.gz rails-68398838544a778244e6028f2c30deb7313ea0b2.tar.bz2 rails-68398838544a778244e6028f2c30deb7313ea0b2.zip |
Break up misc Object extensions
Diffstat (limited to 'activesupport/lib/active_support/core_ext/object/returning.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/returning.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/returning.rb b/activesupport/lib/active_support/core_ext/object/returning.rb new file mode 100644 index 0000000000..0dc2e1266a --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/returning.rb @@ -0,0 +1,42 @@ +class Object + # 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'] + def returning(value) + yield(value) + value + end +end |