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 1 year has passed since last update.

変数の後ろにカンマがついている

Posted at

##変数の後ろにカンマがついている問題があったので備忘録として記しておきます

main.rb
#変数の後ろにカンマを付ける
a, = (1..5).partition(&:odd?)
p a   => [1, 3, 5]

#カンマを付けない
b = (1..5).partition(&:odd?)
p b   => [[1, 3, 5], [2, 4]]

カンマを後ろに付けることで多重代入になる。今回の問題でいうとカンマを付けることで2個目の変数の枠が用意されるが2個目の変数を宣言していないことによって[2, 4]の配列がなくなり、[1, 3, 5]が表示される。

partitionメソッドは指定した条件に一致するか否かで配列を分割します。

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?