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?

【Ruby】可変長引数と可変長キーワード引数

Posted at

RailsのActiveRecord::QueryMethods#includesメソッドがどのような引数を受け取るか調査しました。以下のコードで確認できるように、このメソッドは3種類の引数を受け付けます。

ExampleModel.method(:includes).parameters
=> [[:rest, :*], [:keyrest, :**], [:block, :&]]

メソッドが受け付ける引数は以下です。

  • 可変長引数
  • 可変長キーワード引数
  • ブロック

Rubyの仕様として、これらの引数は以下の順序で指定する必要があります.

  1. 可変長引数を指定
  2. 可変長キーワード引数を指定

以下は誤った例で、この順序に違反するとエラーとなります。

includes(key1: :value1, key2: value2, :value3)
# SyntaxError: ordinary parameter is defined after rest parameter
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?