Eduard
Eduard

Reputation: 61

How to initialize all the values of a two dimensional array using Ruby, is there any way to do it in 1 step?

For example, initialize the entire array to 0 or any other value.

[
  [0, 0, 0, 0],
  [0, 0, 0, 0], 
  [0, 0, 0, 0]
]

Upvotes: 1

Views: 56

Answers (1)

Ursus
Ursus

Reputation: 30056

Sure

Array.new(3) { Array.new(4, 0) }
 => [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] 

Upvotes: 3

Related Questions