💎
Ruby 3.2 - キーワードパラメータその3
Ruby 3.2 アドベントカレンダーの2日目の記事です。
キーワードパラメータその3
Ruby 2.7 ではこういうスクリプトが動いたんだけど、
def hoge(*args)
fuga(*args)
end
def fuga(x: nil)
pp x #=> 123
end
hoge(x: 123)
Ruby 3.0 でキーワードパラメータの仕様が変わってエラーになるようになった。
x.rb:5:in `fuga': wrong number of arguments (given 1, expected 0) (ArgumentError)
メソッドに ruby2_keywords
をつけると Ruby 3.0 でもエラーにならなくなる。
ruby2_keywords def hoge(*args)
fuga(*args)
end
def fuga(x: nil)
pp x #=> 123
end
hoge(x: 123)
キーワードパラメータを受け付けるメソッド(ここでは fuga
)までの間にもうひとつメソッド(ここでは hoge2
)を挟んでも動いた。
ruby2_keywords def hoge(*args)
hoge2(*args)
end
def hoge2(*args)
fuga(*args)
end
def fuga(x: nil)
pp x #=> 123
end
hoge(x: 123)
けど、どうやらこれはバグだったらしい。Ruby 3.2 ではエラーになるようになった。
x.rb:9:in `fuga': wrong number of arguments (given 1, expected 0) (ArgumentError)
hoge2
にも ruby2_keywords
を指定すると動く。
ruby2_keywords def hoge(*args)
hoge2(*args)
end
ruby2_keywords def hoge2(*args)
fuga(*args)
end
def fuga(x: nil)
pp x #=> 123
end
hoge(x: 123)
しかし自分はこの辺は全然わかってないな…。別にわかる必要もない気はする。
Discussion