Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 859987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:45:05+00:00 2026-05-15T08:45:05+00:00

I have a method that returns an array of arrays. For convenience I use

  • 0

I have a method that returns an array of arrays.
For convenience I use collect on a collection to gather them together.

arr = collection.collect {|item| item.get_array_of_arrays}

Now I would like to have a single array that contains all the arrays.
Of course I can loop over the array and use the + operator to do that.

newarr = []    
arr.each {|item| newarr += item}

But this is kind of ugly, is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:45:06+00:00Added an answer on May 15, 2026 at 8:45 am

    There is a method for flattening an array in Ruby: Array#flatten:

    newarr = arr.flatten(1)
    

    From your description it actually looks like you don’t care about arr anymore, so there is no need to keep the old value of arr around, we can just modify it:

    arr.flatten!(1)
    

    (There is a rule in Ruby that says that if you have two methods that do basically the same thing, but one does it in a somewhat surprising way, you name that method the same as the other method but with an exlamation point at the end. In this case, both methods flatten an array, but the version with the exclamation point does it by destroying the original array.)

    However, while in this particular case there actually is a method which does exactly what you want, there is a more general principle at work in your code: you have a sequence of things and you iterate over it and try to “reduce” it down into a single thing. In this case, it is hard to see, because you start out with an array and you end up with an array. But by changing just a couple of small details in your code, it all of the sudden becomes blindingly obvious:

    sum = 0
    arr.each {|item| sum += item } # assume arr is an array of numbers
    

    This is exactly the same pattern.

    What you are trying to do is known as a catamorphism in category theory, a fold in mathematics, a reduce in functional programming, inject:into: in Smalltalk and is implemented by Enumerable#inject and its alias Enumerable#reduce (or in this case actually Array#inject and Array#reduce) in Ruby.

    It is very easy to spot: whenever you initialize an accumulator variable outside of a loop and then assign to it or modify the object it references during every iteration of the loop, then you have a case for reduce.

    In this particular case, your accumulator is newarr and the operation is adding an array to it.

    So, your loop could be more idiomatically rewritten like this:

    newarr = arr.reduce(:+)
    

    An experienced Rubyist would of course see this right away. However, even a newbie would eventually get there, by following some simple refactoring steps, probably similar to this:

    First, you realize that it actually is a fold:

    newarr = arr.reduce([]) {|acc, el| acc += el }
    

    Next, you realize that assigning to acc is completely unnecessary, because reduce overwrites the contents of acc anyway with the result value of each iteration:

    newarr = arr.reduce([]) {|acc, el| acc + el }
    

    Thirdly, there is no need to inject an empty array as the starting value for the first iteration, since all the elements of arr are already arrays anyway:

    newarr = arr.reduce {|acc, el| acc + el }
    

    This can, of course, be further simplified by using Symbol#to_proc:

    newarr = arr.reduce(&:+)
    

    And actually, we don’t need Symbol#to_proc here, because reduce and inject already accept a symbol parameter for the operation:

    newarr = arr.reduce(:+)
    

    This really is a general pattern. If you remember the sum example above, it would look like this:

    sum = arr.reduce(:+)
    

    There is no change in the code, except for the variable name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method (C++) that returns a character and takes an array of
I have a Ruby method that searches an array of hashes and returns a
I have a method that returns lot of data, should I use @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) for
I have a python method that returns a Python byte array.array('c'). Now, I want
In my class, I have a method that returns an array like this. double
Suppose I have a method that returns an array or list or some other
Hi I have a method that returns an array of support tickets. Each support
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returns module path of given class name def findModulePath(path, className):
I have a method that returns a list of type string. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.