Ruby 2.7 特定警告未显示

发布于 2025-01-19 15:09:52 字数 1003 浏览 3 评论 0原文

我们希望将代码库更新到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2025-01-26 15:09:52

我发现了一些事情。首先,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

另一点是,您稍后可以设置的 $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

# 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

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 only RUBYOPT 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文