To create a Three Dimensional Array in Ruby, you can use arrays or nested arrays.
In this example,
# Creating a 3D Array with Nested Arrays
three_d_array = Array.new(3) { Array.new(2) { Array.new(4, 0) } }
# Modify an element
three_d_array[1][0][2] = 5
# Print the array
three_d_array.each do |outer|
outer.each do |middle|
puts middle.join(' ')
end
puts
end
0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0
In this tutorial, we learned How to create a Three Dimensional Array in Ruby language with well detailed examples.