Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

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の多次元配列初めて使ったけど、他の言語に比べて扱いが難しい気がする。

KaiShoya
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした