tags:

views:

72

answers:

2

Is there a better way for

result = []
function_that_yields{ |value| result << value }
+3  A: 

If the function that yields has no logic to build the array, then this is the only way to accomplish this task. Otherwise, consider using built-in enumerable methods such as inject or map.

Simone Carletti
A: 

Yeah, you can use result = something.inject([]) {|x,y| x << y}

You can push anything into x, so you could do something like x << value in this case.

Chuck Vose