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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:38:36+00:00 2026-05-30T19:38:36+00:00

I just started learning ruby. Now I need to figure out the dimension of

  • 0

I just started learning ruby.
Now I need to figure out the dimension of a multidimensional array. I had a look at ruby-docs for the all the array methods, but I could not find a method that returns the dimension.

Here is an example:

For [[1, 2],[3,4],[5,6]] the dimension should be 2.

For [[[1,2],[2,3]],[[3,4],[5]]], the dimension should be 3.

  • 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-30T19:38:37+00:00Added an answer on May 30, 2026 at 7:38 pm

    There is not a built-in function for that as there may be multiple definition as to what you mean by “dimension” for an array. Ruby’s arrays may contain anything including hashes or other arrays. That’s why I believe you need to implement your own function for that.

    Asuming that by dimension you mean “the deepest nested level of arrays” this should do the trick:

    def get_dimension a
      return 0 if a.class != Array
      result = 1
      a.each do |sub_a|
        if sub_a.class == Array
          dim = get_dimension(sub_a)
          result = dim + 1 if dim + 1 > result
        end
      end
      return result
    end
    

    EDIT: and as ruby is a great language and allows you to do some fancy stuff you can also make get_dimension a method of Array:

     class Array
       def get_dimension
       ... # code from above slightly modified
       end
     end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started learning Lisp and I can't figure out how to compile and
I have just started learning Ruby on Rails. I happened to look for prevention
I have started learning Ruby and just tried out my first hello world program
I have just started out learning rails and ruby, so please bear with me
I have just started learning Erlang and am trying out some Project Euler problems
I just started learning Ruby. I typed the example : x = 10 5.times
I have just started learning ruby on rails. I have a doubt wrt routing.
I've just started learning Ruby on Rails and it's a newbie question. I did
I've only just started learning ruby on rails and I would like to create
Just started learning Scheme. I'm using Dr. Racket as my compiler/interpreter. I need some

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.