Selvam
Selvam

Reputation: 1093

Ruby: 2 dimension arrays

How do you declare a 2 dimension array in Ruby. I know, V=[] is one dimensional. But v=[][] for 2 dimensional? And in a block I want to add values as subarrays in a array. i.e. V=[["ab","ba"],["12","21"]]. This is what I am doing. Let x=[]. I take each element, store the original as well as the reverse.

x.each{|k| l=k_reverse v=(k,l)}

Upvotes: 1

Views: 976

Answers (1)

Aditya Sanghi
Aditya Sanghi

Reputation: 13433

# Given
list = ["ab","12"]
# This should give you an array of arrays
v = list.map{|x| [x,x.reverse] }
# v = [["ab","ba"],["12","21"]]

A 2 dimensional array could possibly be initialized as

v = [[]] # not [][], [][] would be the reader for a 2 dim array

Upvotes: 4

Related Questions