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 7972175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:47:48+00:00 2026-06-04T07:47:48+00:00

One of the things I commonly get hooked up on in ruby is recursion

  • 0

One of the things I commonly get hooked up on in ruby is recursion patterns. For example, suppose I have an array, and that may contain arrays as elements to an unlimited depth. So, for example:

my_array = [1, [2, 3, [4, 5, [6, 7]]]]

I’d like to create a method which can flatten the array into [1, 2, 3, 4, 5, 6, 7].

I’m aware that .flatten would do the job, but this problem is meant as an example of recursion issues I regularly run into – and as such I’m trying to find a more reusable solution.

In short – I’m guessing there’s a standard pattern for this sort of thing, but I can’t come up with anything particularly elegant. Any ideas appreciated

  • 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-06-04T07:47:50+00:00Added an answer on June 4, 2026 at 7:47 am

    Recursion is a method, it does not depend on the language. You write the algorithm with two kind of cases in mind: the ones that call the function again (recursion cases) and the ones that break it (base cases). For example, to do a recursive flatten in Ruby:

    class Array
      def deep_flatten
        flat_map do |item|
          if item.is_a?(Array)
            item.deep_flatten
          else
            [item]
          end
        end
      end
    end 
    
    [[[1]], [2, 3], [4, 5, [[6]], 7]].deep_flatten
    #=> [1, 2, 3, 4, 5, 6, 7]
    

    Does this help? anyway, a useful pattern shown here is that when you are using recusion on arrays, you usually need flat_map (the functional alternative to each + concat/push).

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

Sidebar

Related Questions

i notice one nice things that a amazon provide that is load pdf data
One of the things that block objects, introduced in Snow Leopard, are good for
This is one of those things that you read once, say aha! and then
Recently I found out that there are several things that one can do to
Simple, probably dumb question: Suppose I have a Java server that stores in memory
i have noticed one things , when some website are opened in any browser
I've heard that there are some things one cannot do as a computer programmer,
One of the things I would like to follow is to stick to the
One of the things I love is the new Android Favorites Contact screen: I
I am looking for one of 2 things, Test Driven Development, or Architecture Trainings

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.