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?

unshift(可変長引数)

Posted at

後から順にオブジェクトを配列の先頭に代入する

unshift(*obj) -> self
prepend(*obj) -> self
指定された obj を引数の最後から順番に配列の先頭に挿入します。引数を指定しなければ何もしません。

[PARAM] obj:
自身に追加したいオブジェクトを指定します。

irb(main):120> arr.unshift 0
irb(main):121> p arr             #=> [0, 1, 2, 3]
irb(main):122> arr.unshift [0]
irb(main):123> p arr             #=> [[0], 0, 1, 2, 3]
irb(main):124> arr.unshift 1, 2
irb(main):125> p arr             #=> [1, 2, [0], 0, 1, 2, 3]
[0, 1, 2, 3]
[[0], 0, 1, 2, 3]
[1, 2, [0], 0, 1, 2, 3]
=> [1, 2, [0], 0, 1, 2, 3]

色々格納できる

irb(main):129> arr.unshift Enumerator.new {}
=> [#<Enumerator: ...>, 1, 2, [0], 0, 1, 2, 3]

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?