Class: Object

Inherits:
BasicObject
Defined in:
lib/core_exts/object.rb

Overview

Swiped from Rails’ ActiveSupport 2.3.2

Instance Method Summary collapse

Instance Method Details

#returning(value) {|value| ... } ⇒ 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']

Yields:

  • (value)


40
41
42
43
# File 'lib/core_exts/object.rb', line 40

def returning(value)
  yield(value)
  value
end