13
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

簡単な二次元配列の作り方

ググってもなかなかピンとくる書き方が無かったため、メモ。
やっていることは、一次元配列に一次元配列を格納してるだけ。

arr = []
#適当に3回ループ
3.times do
    val1 = "a"
    val2 = "b"
    arr << [val1, val2]
end

配列の中身

[["a","b"],["a","b"],["a","b"]]

これで、String[2,3]の二次元配列ができる。

取り出すときは、

arr.each do | val1, val2 |
    p val1
    p val2
end

みたいに、doの後ろの||の中を増やしてあげれば簡単に取り出せる。

Rubyの多次元配列初めて使ったけど、他の言語に比べて扱いが難しい気がする。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
13
Help us understand the problem. What are the problem?