百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

ruby-warning

> 编程语言
开源

为警告添加自定义处理

302 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

为警告添加自定义处理

= ruby-warning

ruby-warning adds custom processing for warnings, including the ability to ignore specific warning messages, ignore warnings in specific files/directories, include backtraces with warnings, treat warnings as errors, deduplicate warnings, and add custom handling for all warnings in specific files/directories.

ruby-warning requires ruby 2.4+, as previous versions of ruby do not support custom processing of warnings.

= Installation

gem install warning

= Source Code

Source code is available on GitHub at https://github.com/jeremyevans/ruby-warning

= Usage

By default, requiring the library does not make changes to how ruby processes warnings, it just adds methods that allow you to customize the processing.

Warning.ignore takes a regexp and optionally a path prefix, and ignores any warning that matches the regular expression if it starts with the path prefix. It can also take a symbol or an array of symbols, and will use an appropriate regexp. The supported symbols are:

  • :arg_prefix
  • :ambiguous_slash
  • :bignum
  • :default_gem_removal
  • :fixnum
  • :ignored_block
  • :keyword_separation
  • :method_redefined
  • :mismatched_indentations
  • :missing_gvar
  • :missing_ivar
  • :not_reached
  • :safe
  • :shadow
  • :taint
  • :unused_var
  • :useless_operator
  • :void_context

Warning.process takes an optional path prefix and a block, and if the warning string starts with the path prefix, it calls the block with the warning string instead of performing the default behavior. You can call Warning.process multiple times and it will operate intelligently, choosing the longest path prefix that the string starts with.

Warning.process blocks can return +:default+ to use the default behavior, +:backtrace+ to use the default behavior and also print the backtrace or +:raise+ to raise the warning string as a +RuntimeError+. You can use Warning.error_class= to override the exception class used.

Warning.process can also accept a hash of actions instead of a block, with keys being regexps (or symbols supported by Warning.ignore) and values being callable objects (or +:default+, +:backtrace+, or +:raise+).

Warning.dedup deduplicates warnings, so that if a warning is received that is the same as a warning that has already been processed, the warning is ignored. Note that this should be used with care, since if the application generates an arbitrary number of unique warnings, that can lead to unbounded memory growth.

Warning.clear resets the library to its initial state, clearing the current ignored warnings and warning processors, and turning off deduplication.

By using path prefixes, it's fairly easy for a gem to set that specific warnings should be ignored inside the gem's directory.

Note that path prefixes will not correctly handle warnings raised by Kernel#warn, unless the warning message given to Kernel#warn starts with the filename where the warning is used. The Kernel#warn +:uplevel+ option will make sure the warning starts with the filename.

Note that many of the warnings this library can ignore are warnings caused during compilation (i.e. when files are loaded via require). You should require this library and setup the appropriate warning handling before loading any code that could cause warnings.

= Examples

Ignore all uninitialized instance variable warnings

Warning.ignore(/instance variable @\w+ not initialized/)

Ignore all uninitialized instance variable warnings in current file

Warning.ignore(/instance variable @\w+ not initialized/, FILE)

Ignore all uninitialized instance variable warnings in current file

Warning.ignore(:missing_ivar, FILE)

Ignore all Fixnum and Bignum warnings in current file

Warning.ignore([:fixnum, :bignum], FILE)

Write warning to LOGGER at level warning

Warning.process do |warning| LOGGER.warning(warning) end

Write warnings in the current file to LOGGER at level error

Warning.process(FILE) do |warning| LOGGER.error(warning) end

Write warnings in the current file to $stderr, but include backtrace

Warning.process(FILE) do |warning| :backtrace end

Raise warnings in the current file as RuntimeErrors, with the warning

string as the exception message

Warning.process(FILE) do |warning| :raise end

Raise keyword argument separation warnings in the current file as

RuntimeErrors, and write ambiguous slash warnings to $stderr, including

the backtrace

Warning.process(FILE, keyword_separation: :raise, ambiguous_slash: :backtrace)

Deduplicate warnings

Warning.dedup

Ignore all warnings in Gem dependencies

Gem.path.each do |path| Warning.ignore(//, path) end

= License

MIT

= Author

Jeremy Evans

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言