Selectively silence JRuby warnings

余生颓废 提交于 2019-12-06 05:10:28

You have a couple options.

First, you can run your program with the -W0 option which will disable all warnings. That's probably not what you want.

However, applying -W0 is the same as setting $VERBOSE to nil -- so we can simply do that around the code where we want to suppress warnings. This is the second and much more preferable option.

def suppress_all_warnings
  old_verbose = $VERBOSE
  begin
    $VERBOSE = nil
    yield if block_given?
  ensure
    # always re-set to old value, even if block raises an exception
    $VERBOSE = old_verbose
  end
end

puts "Starting"
MyConst = 1
MyConst = 2
suppress_all_warnings do
  GC.disable
end
puts "Done"

Running this with JRuby 1.5.0 correctly warns me about the reinitialized constant and correctly suppresses the GC.disable warning.

If you switch from ruby-mysql to activerecord-jdbcmysql-adapter, you can avoid this warning entirely.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!