LoginSignup
8
7

More than 5 years have passed since last update.

Rubyの二次元配列について

Posted at

Rubyの二次元配列に要素を追加するときにハマったのでメモ

普通にpushすると以下のようになる。

[18] pry(main)> test = Array.new(4, Array.new)
=> [[], [], [], []]
[19] pry(main)> test[0].push(1)
=> [1]
[20] pry(main)> test
=> [[1], [1], [1], [1]]

ので以下のようにした。

[21] pry(main)> test[0] = test[0] + [1]
=> [1, 1]
[22] pry(main)> test
=> [[1, 1], [1], [1], [1]]

でもこれ絶対おかしいと思うので詳しい方もっといい方法あれば教えてください。

8
7
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7