[英]After upgrading to Ruby 3, unable to pass multiple arguments to my "initialize" method
我最近使用 Ruby 3 升級到 Rails 6。我在控制器中有這個
my_object = MyObject.new(my_object_params, @header)
對象“初始化”方法的定義如下
def initialize(params, header)
super(params)
user&.header = header
end
但是現在在嘗試初始化對象時,我得到了錯誤
ArgumentError:
wrong number of arguments (given 2, expected 0..1)
Ruby 3 在初始化期間將多個參數傳遞給對象的正確方法是什么?
你需要這樣做:
def initialize(params, header)
super(**params) #Note that you need to splat out the params!
user&.header = header
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.