Ruby 2.7 特定警告未显示
我们希望将代码库更新到 Ruby 3,最大的突破性变化之一是关键字参数与方法中参数的混合。这个警告应该会出现,
warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
但我注意到在运行一个简单的脚本时,它只是没有出现
# 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>'
它在 Ruby 3 中按计划中断,但在以前的版本中没有显示任何内容。
这种行为在我们的生产中是相同的,我在任何地方都找不到发生的情况。我使用过 RUBYOPT='-W:deprecated'
甚至 $VERBOSE = true
成功列出了除此之外的其他几个弃用警告。
看了一圈,其他人好像没有这个问题。我在这里缺少什么吗? Ruby 中有选项吗?使用其他版本的 Ruby(例如 2.7.2)呈现相同的效果。
另一个细节,我们使用 Ruby on Rails 来运行一切,但我相信问题出在 Ruby 本身。
We want to update our codebase to Ruby 3 and one of the biggest breaking change is the mix of keyword arguments with arguments in methods. This warning should show up
warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
But I've noticed when running a simple script, it just doesn't appear
# 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>'
It breaks as planned in Ruby 3, but doesn't show anything in the previous versions.
This behavior is the same in production for us, I couldn't spot one occurrence anywhere. I've used RUBYOPT='-W:deprecated'
or even $VERBOSE = true
that successfully list several other deprecation warnings except this one.
After looking around, it doesn't seem other people have this problem. Is there something I am missing here? Is there an option in Ruby? Using other versions of Ruby like 2.7.2 renders the same.
Another detail, we use Ruby on Rails ton run everything, but I believe the problem is within Ruby itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了一些事情。首先,Ruby 2.7.2 及更高版本默认情况下不显示警告,这可以解释我的测试中行为的差异。要激活它们,您必须使用
RUBYOPT
另一点是,您稍后可以设置的
$VERBOSE = true
也无法显示此警告。我不确定具体原因,但只有RUBYOPT
可以解决这个特定问题。最后,在本地启动 Rails 服务器时,我还错误地使用了 RUBYOPT 选项,因此看不到它工作。
我希望它有帮助。
I've discovered a few things. First of all, Ruby 2.7.2 and later don't show warnings by default which would explain the difference of behavior in my tests. To activate them you have to use
RUBYOPT
Another point is that the
$VERBOSE = true
you can set later on doesn't work either to show this warning. I'm not sure exactly why, but onlyRUBYOPT
would do the job for this specific problem.Finally, I also had used wrongly the
RUBYOPT
option when starting my Rails server locally thus not seeing it working.I hope it helps.