How to get the length of rows and columns of a two-dimensional array in rails 3?

can anyone tell how to get the length of the row and column of a two-dimensional array in rails 3?

My array looks like this:


 payroll = Array.new[Payroll.count][2]

When we get the length of the 1-dimensional array, we like

 array.length 
How about in a two dimensional array?

I am thinking of doing something like:


 payroll = Array.new[Payroll.count][2]

 for i in 0..payroll.row.length - 1
  for j in 0..1
   puts payroll[i][j]
  end
 end

I just want to know the right way. Help Pls ...

+3
source share
1 answer

A two-dimensional array is just an array of arrays, so just use payroll.lengthto get the height and payroll[0].lengthto get the width (assuming all rows have the same width). Here's what your loop looks like using this idea:

for i in 0..payroll.length - 1
  for j in 0..payroll[i].length - 1
    puts payroll[i][j]
  end
end

- . for each.with_index ( each_with_index, Ruby each.with_index):

payroll.each.with_index do |row, i|
  row.each.with_index do |cell, j|
    puts payroll[i][j]
  end
end

, , , :

payroll.each do |row|
  row.each do |cell|
    puts cell
  end
end
+5

All Articles