0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ruby】配列の中に要素を追加する方法。pushと<<(破壊的処理)

Posted at

Rubyである配列の後ろに要素を足していく処理は、pushまたは<<で実現できる。

<<が初見だったので意味を調べてみたところ、配列への要素追加処理だった。

.push

pushメソッド使う方法。<<と異なり、引数を複数とれる

push
x = {a:1, b:2}
y = {c:3, d:4}

arr = []

arr.push(x, y)
=> [{:a=>1, :b=>2}, {:c=>3, :d=>4}]

## << pushと異なり引数は1つしか取れないが、つなげて記述することができる。
<<
x = {a:1, b:2}
y = {c:3, d:4}

arr = []

arr << x << y
=> [{:a=>1, :b=>2}, {:c=>3, :d=>4}]


文字数が少ないので`<<`を使っている人が多いかな。
0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?