gpt4 book ai didi

ruby - 如何选择要显示警告的文件?

转载 作者:数据小太阳 更新时间:2023-10-29 07:19:22 24 4
gpt4 key购买 nike

要用 Ruby 显示警告,可以使用标志 -w 调用 ruby。使用库时,Ruby 可能会显示很多警告,因此很难从您实际处理的文件中找到警告。是否可以告诉 ruby 仅对某些文件显示警告?

这里举个例子来说明问题。下面加载了一个生成大量警告的库,它还通过重新定义常量自行生成警告。

require 'grooveshark'

CONSTANT = 'foo'
CONSTANT = 'bar'

ruby -w warning_test.rb输出如下:

/var/lib/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/exceptions.rb:157: warning: assigned but unused variable - message/var/lib/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/exceptions.rb:167: warning: assigned but unused variable - message/var/lib/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/response.rb:11: warning: method redefined; discarding old body/var/lib/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/payload.rb:47: warning: mismatched indentations at 'end' with 'case' at 40/var/lib/gems/1.9.1/gems/macaddr-1.6.1/lib/macaddr.rb:63: warning: assigned but unused variable - status/var/lib/gems/1.9.1/gems/macaddr-1.6.1/lib/macaddr.rb:63: warning: assigned but unused variable - stderr/var/lib/gems/1.9.1/gems/macaddr-1.6.1/lib/macaddr.rb:56: warning: assigned but unused variable - re/var/lib/gems/1.9.1/gems/macaddr-1.6.1/lib/macaddr.rb:59: warning: assigned but unused variable - null/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:126: warning: shadowing outer local variable - cid/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:213: warning: shadowing outer local variable - buf/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:215: warning: shadowing outer local variable - buf/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:208: warning: assigned but unused variable - ignored/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:252: warning: shadowing outer local variable - key/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:249: warning: assigned but unused variable - ignored/var/lib/gems/1.9.1/gems/grooveshark-0.2.7/lib/grooveshark/user.rb:65: warning: method redefined; discarding old playlists/var/lib/gems/1.9.1/gems/grooveshark-0.2.7/lib/grooveshark/user.rb:93: warning: method redefined; discarding old favoriteswarning_test.rb:4: warning: already initialized constant CONSTANT

在这种情况下,我只想看到最后一个警告

warning_test.rb:4: warning: already initialized constant CONSTANT

由于其他警告是由于库引起的,所以我无法直接控制这些警告,因为它们并不那么有趣。我希望能够告诉 Ruby 只显示正在运行的项目的警告。

我意识到定义电流可能很棘手。也许只显示给定文件和通过 require_relative 包含的文件的警告就可以了。另一种选择是对目录结构中接近给定文件的文件显示警告。

最佳答案

您可以将 stderr 重定向到 stdout,然后再 grep。这是一个例子:

ruby -w warning_test.rb 2>&1 | grep“warning_test”

关于ruby - 如何选择要显示警告的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com