禁止 Ruby 中的特定警告 [英] Suppress particular warning in Ruby

查看:40
本文介绍了禁止 Ruby 中的特定警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多帖子提供了 -W0 标志作为这个问题的答案,但我不想抑制所有警告,只是抑制特定值的警告.

I've seen plenty of posts providing the -W0 flag as an answer to this issue, but I don't want to suppress all warnings, just warnings of a particular value.

我正在 Ruby 1.8.7 上运行一个非 Rails 应用程序(尽管使用 ActiveRecord).我想保留除以下弃用警告之外的所有警告:

I'm running a non-rails app (which uses ActiveRecord, notwithstanding) on Ruby 1.8.7. I want to keep all warnings except for the following DEPRECATION WARNING:

Object#id 将被弃用;使用对象#object_id

如果这是不可能的,我想放弃所有弃用警告.至少,Java 可以让您做到这一点.红宝石怎么样?

If that's not possible, I'd like to jettison all deprecation warnings. Java, at least, lets you do this. How about Ruby?

更新:我对这两个答案都投了赞成票,但检查了以后搜索者希望在这里找到的那​​个.

Update: I've upvoted both answers but checked the one that later searchers will expect to find here.

推荐答案

如果有特定的代码部分产生警告,您可以尝试在 ActiveSupport 并用 silence_warnings 块包装它(示例直接从 RDoc 中提取):

If there's a specific section of code that produces the warnings, you could try mixing in the Kernel module from ActiveSupport and wrap it with a silence_warnings block (example pulled straight from the RDoc):

silence_warnings do
  value = do_something_that_causes_warning # no warning voiced
end

noisy_call # warning voiced

绝对有必要压制吗?这并不是说您正在编译某些东西并且必须一次筛选大量警告......

Is it absolutely necessary to suppress it? It's not like you're compiling something and have to sift through a ton of warnings all at once...

这篇关于禁止 Ruby 中的特定警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆