1

i created two dimensional array with the following code. but i can not store value in the array. row and columns of array grows dynamically. can not predict before.

arr = Array.new {Array.new}

and can not do this type of things.....

arr[0][0] = "Ruby" 
0

1 Answer 1

0

You could create an abstraction over the standard array, like so

class MyArray < Array
    def [](idx)
        self.at(idx) ? self.at(idx) : self[idx] = []
    end
end

Alternatively, you could use a Hash whose default_proc creates a new array at the specified index. OR a hash whose keys are [row, column]. This would be the best option for large datasets as your operations would be in O(1) time.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.