method
returning
v3.0.9 -
Show latest stable
-
2 notes -
Class: Object
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3 (0)
- 2.1.0 (0)
- 2.2.1 (23)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-4)
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
returning(value)
public
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']
Register or
log in
to add new notes.
Mange -
January 8, 2009
5 thanks
Watch out for syntax errors
Watch out when you are using returning with hashes. If you would write code like
def foo(bars) returning {} do |map| bars.each { |bar| map[bar.first] = bar } end end
you will get a syntax error since it looks like you tried to supply two blocks! Instead you should write it with parenthesis around the hash:
def foo(bars) returning({}) do |map| bars.each { |bar| map[bar.first] = bar } end end