2
所以我一直在Traiblazer和改革文件,我經常看到這樣的代碼這個代碼示例中的double splat(**)參數是什麼意思,爲什麼使用它?
class AlbumForm < Reform::Form
collection :songs, populate_if_empty: :populate_songs! do
property :name
end
def populate_songs!(fragment:, **)
Song.find_by(name: fragment["name"]) or Song.new
end
end
通知的def populate_songs!(fragment:, **)
定義是什麼?
我很清楚雙啪啪命名參數(如**others
)捕獲所有其他關鍵字參數。但我從來沒有見過**
,沒有名字。
所以我的2個問題是:
- 什麼**在上面的塊是什麼意思?
- 爲什麼使用這種語法?
同樣的模式也適用於常規參數。單個splat參數將接受任何參數,但不會將它們分配給變量。 –
@HolgerJust:是的,當然,謝謝你的提及。 –