Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
次のコードで2次元配列を作成しました。しかし、配列に値を格納できません。配列の行と列は動的に拡大します。事前に予測することはできません。
arr = Array.new {Array.new}
この種のことはできません.....
arr[0][0] = "Ruby"
次のように、標準配列に対して抽象化を作成できます
class MyArray < Array def [](idx) self.at(idx) ? self.at(idx) : self[idx] = [] end end
default_procまたは、指定されたインデックスで新しい配列を作成するHash を使用することもできます。または、キーが [row, column] であるハッシュ。操作は O(1) 時間になるため、これは大規模なデータセットに最適なオプションです。
default_proc