0
0

More than 1 year has passed since last update.

配列のコピー(Ruby)

Last updated at Posted at 2022-01-02

Ruby3.0.0
class Array

#配列の作成(配列の長さ,値):全要素が同じ値を参照する。値が複製される訳ではないことに注意
a = Array.new(3,"foo")
#=> ["foo","foo","foo"]

#配列のコピー:要素を複製しない浅い複製
b = Array.new(a)
#=> ["foo","foo","foo"]

#全要素を複製する:各要素は違うオブジェクト
c = Array.new(3){"foo"}
#=> ["foo","foo","foo"]
c[0] = "fo"
#=> ["fo","foo","foo"]
0
0
2

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
0
0