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

  • Home
  • SEARCH
  • 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 7435989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:10:01+00:00 2026-05-29T10:10:01+00:00

I want a twodimensional array in Ruby, that I can access for example like

  • 0

I want a twodimensional array in Ruby, that I can access for example like this:

if @array[x][y] == "1" then @array[x][y] = "0"

The problem is: I don’t know the initial sizes of the array dimensions and i grow the array (with the << operator).

How do I declare it as an instance variable, so I get no error like this?

undefined method `[]' for nil:NilClass (NoMethodError)

QUESTION UPDATED:

@array = Array.new {Array.new} 

now works for me, so the comment from Matt below is correct!

I just found out the reason why I received the error was because I iterated over the array like this:

for i in 0..@array.length
    for j in 0..@array[0].length
        @array[i][j] ...

which was obviously wrong and produced the error. It has to be like this:

for i in 0..@array.length-1
        for j in 0..@array[0].length-1
            @array[i][j] ...
  • 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-29T10:10:02+00:00Added an answer on May 29, 2026 at 10:10 am

    A simple implementation for a sparse 2-dimensional array using nested Hashes,

    class SparseArray
      attr_reader :hash
    
      def initialize
        @hash = {}
      end
    
      def [](key)
        hash[key] ||= {}
      end
    
      def rows
        hash.length   
      end
    
      alias_method :length, :rows
    end
    

    Usage:

    sparse_array = SparseArray.new
    sparse_array[1][2] = 3
    sparse_array[1][2] #=> 3
    
    p sparse_array.hash
    #=> {1=>{2=>3}}
    
    #
    # dimensions
    #
    sparse_array.length    #=> 1
    sparse_array.rows      #=> 1
    
    sparse_array[0].length #=> 0
    sparse_array[1].length #=> 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a two-dimensional array without an initialized length like this: Matrix
I want to define a dynamic two-dimensional array in python. I don't know how
want to know why String behaves like value type while using ==. String s1
I want to create a two dimensional array dynamically. I know the number of
I have a 2D-array that I want to sort based on the second column.
I want to decalare an two dimensional array of buttons..so can anyone please suggest
I have an array of strings that I want to sort in order of
I have created a multidimensional array in Python like this: self.cells = np.empty((r,c),dtype=np.object) Now
How can I define two dimensional dynamic array? If I want to use List<>,
I want to know how to declare a two dimensional array in Python. arr

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.