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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:36:58+00:00 2026-05-26T22:36:58+00:00

So I wrote a 2D array class for a project I’m working on, in

  • 0

So I wrote a 2D array class for a project I’m working on, in Ruby.

It’s simple enough: the initialize creates a Square 2D array, of a size dictated by its argument, called @contents.

I overload the [] operator to make the data within the object accessible, like so:

def [] (x, y)
  @contents[x][y]
end

def []=(x, y, z)
  @contents[x][y] = z
end

I add an each method to it, so that I could mix in the enumerable module, and give it some functionality, like so:

def each
  i, j =0,0
  while (i<@size)
    while(j<@size)
      yield @contents[i][j]
      j+=1
    end
    i, j = i+1, 0
  end
end

and I gave it a to_s method, like so:

def to_s
  @contents.each{|i| puts i}
end

Now, I create a new instance of this 2D array object in a new file, and try to populate it. Initally,I did this manually, with nested loops, like so:

j, i, k= 0, 0, 0
puts " size = #{@size}"
while (i<@size)
  while(j<@size)
    @2Darray[i,j], k, j= @array[k], k+1, j+1
  end
  i, j=i+1, 0
end

And that works just fine. the to_s method will print out the array perfectly. I realized though, that these nested loops might not have been the ‘ruby way’ and tried to do it with itterators. I had already implimented an each method, so I tried to subsitute the above, with the following:

j=0
@2Darray.each{|i|
  i = @array[j]
  j += 1
  break unless j < size
}

But, unfortunately, this doesn’t work. It runs just fine, but when the to_s is called, it prints line after line of nothing. ie, it prints out an array of empty arrays.

I thought it was my .each method, but that works fine when the to_s method calls it. I thought it might have been my []= method, but that works fine when I use nested while-loops.

Now I’m thinking it must be a problem with scoping. in my each-loop above, |i| is a local variable, am I right? So that I assign the value of array[j] to i, but i is just a copy of the (empty) position which I want to fill, yes?

So, firstly, am I on the right track, or is my problem somewhere else entirely? If so, where, if not: How can I modify my each-loop above, to make assignments to the data within @2Darray, instead of just changing the value of the |i| place-holder.

Apologies for my verbouseness, and thank you for your time! Any help would be 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-05-26T22:36:58+00:00Added an answer on May 26, 2026 at 10:36 pm
    @2Darray.each{|i| i=@array[j]
    

    That is not the way you can populate whatever @2Darray is. i is a block local variable. The #each method effectivelly calls your block (the iterator) for every element, passing a reference to the element in that block local variable. Assigning to a variable will not change the object the variable had been pointing to previously.

    The “ruby way” to populate it would be to provide an instance method that accepts the data to be used, that then uses whatever loop is appropriate.

    For what it’s worth, your while(i<@size) loops can well be expressed as @size.times

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

Sidebar

Related Questions

For my project I wrote a small config class that loads its data from
I'm trying to sort an array, filled with objects from a class i wrote.
I am working on a project for a class. We are to write a
I wrote a nice little array class in C++ that uses void* to save
I wrote two queries to find duplicates in the array var groups = from
i have wrote a script to produce an array of data but now want
I am using C and I have an array t[] I wrote a for
here's the stored procedure i wrote.In this proc p_subjectid is an array of numbers
How can I write every element of array of unspecified rank and size? So
Since I need to add an operator& for the std::tr1::array<bool, N> I wrote the

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.