Lau*_*ent 7 ruby ruby-on-rails deprecation-warning
我们希望将代码库更新到 Ruby 3,最大的突破性变化之一是关键字参数与方法中参数的混合。这个警告应该会出现
warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
Run Code Online (Sandbox Code Playgroud)
但我注意到在运行一个简单的脚本时,它只是没有出现
# script.rb
def my_method(argument, other:)
puts "hello"
end
options = { other: "medium" }
argument = true
my_method(argument, options)
$ rvm use 2.7.5
$ ruby script.rb
hello
$ rvm use 3.0.1
$ ruby script.rb
script.rb:1:in `my_method': wrong number of arguments (given 2, expected 1; required keyword: other) (ArgumentError)
from script.rb:8:in `<main>'
Run Code Online (Sandbox Code Playgroud)
它按计划在 Ruby 3 中中断,但在以前的版本中没有显示任何内容。
这种行为在我们的生产中是相同的,我在任何地方都找不到发生的情况。我已经使用过RUBYOPT='-W:deprecated'
,甚至$VERBOSE = true
成功列出了除此之外的其他几个弃用警告。
查了一下其他人好像没有这个问题。我在这里缺少什么吗?Ruby 中有选项吗?使用其他版本的 Ruby(例如 2.7.2)呈现相同的效果。
另一个细节,我们使用 Ruby on Rails 来运行一切,但我相信问题出在 Ruby 本身。
我发现了一些事情。首先,Ruby 2.7.2 及更高版本默认情况下不显示警告,这可以解释我的测试中行为的差异。要激活它们,您必须使用RUBYOPT
# Ruby 2.7.5
$ RUBYOPT='-W:deprecated' ruby script.rb
script.rb:8: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
script.rb:1: warning: The called method `my_method' is defined here
hello
Run Code Online (Sandbox Code Playgroud)
另一点是,$VERBOSE = true
您稍后可以设置的也无法显示此警告。我不确定到底为什么,但只会RUBYOPT
针对这个特定问题完成这项工作。
最后,我在本地启动 Rails 服务器时也错误地使用了该RUBYOPT
选项,因此看不到它工作。
我希望它有帮助。