1

Is there a better way for

result = []
function_that_yields{ |value| result << value }

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.