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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:37:35+00:00 2026-06-12T23:37:35+00:00

I followed these instructions to create a custom class for two dimensional arrays. class

  • 0

I followed these instructions to create a custom class for two dimensional arrays.

class SparseArray
  attr_reader :hash

  def initialize
    @hash = {}
  end

  def [](key)
    hash[key] ||= {}
  end

  def rows
    hash.length   
  end

  alias_method :length, :rows
end

How can I modify this class so I can iterate through the first and second level of an object using Object#each do? Please explain in simple terms, I’m a newbie.

Example of how I would use the each method on an object:

sparse_array[0][0] = "row 1 column 1"
sparse_array[0][1] = "row 1 column 2"
sparse_array[1][0] = "row 2 column 1"

sparse_array.each do |row|
  # sparse_array[0] on first iteration
  row.each do |column|
    # sparse_array[0][0] on first iteration
  end
end
  • 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-12T23:37:36+00:00Added an answer on June 12, 2026 at 11:37 pm

    You should define each method on your SparseArray and use it to wrap values of the wrapped hash:

    def each &block
      @hash.values.each(&block)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've followed these two walk-throughs: http://a1ashiish-csharp.blogspot.com/2012/01/cnet-how-to-create-wcf-web-service-in.html http://a1ashiish-csharp.blogspot.com/2012/01/cnet-how-to-consume-web-service-in-cnet.html Application X on machine X can now
I followed these instructions from GitHub web page to create a first repo; Global
I followed these instructions while trying to get remote debugging working with Delphi 2007.
I followed these instructions here: http://logback.qos.ch/consolePlugin.html I have the correct and found logback.xml, it
I have followed these instructions exactly, yet I get a Page cannot be found
I followed these two exemples of developer.android (Creating an Input Method, Soft Keyboard sample).
I followed the instructions here on how to create an Android library project, and
I mostly followed these instructions to get things spun up: http://lindsaar.net/2010/5/9/Getting-Rails-3-Edge-with-jQuery-RSpec-and-Cucumber-using-RVM If I create
I have followed the instructions at http://www.javacodegeeks.com/2011/01/advanced-smartgwt-tutorial-part-1.html and tried to create a simple screen
I want to create aa localizable WPF Application. I've followed the instructions in 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.