如何在 Rails 中启用 Ruby 警告?
我在 test.rb
中执行了此操作:
def some_method
p "First definition"
end
def some_method
p "Second definition"
end
some_method
当我调用 ruby test.rb
时,它会打印 SecondDefinition
(预期)
当我调用 时ruby -w test.rb
,它打印第二个定义
(预期)并打印警告test.rb:5:警告:方法重新定义;丢弃旧的 some_method
有没有办法在 Rails 中启用这些警告? (并将警告打印到控制台/日志文件)
为什么我想启用警告:例如,如果我无意中重新定义控制器中的方法,那么我通过查看打印到控制台/日志文件的警告可以意识到问题。 请参阅此处的示例< /a>.
I did this in test.rb
:
def some_method
p "First definition"
end
def some_method
p "Second definition"
end
some_method
When I call ruby test.rb
, it prints Second definition
(expected)
When I call ruby -w test.rb
, it prints Second definition
(expected) and prints a warning test.rb:5: warning: method redefined; discarding old some_method
Is there a way to enable those warnings in Rails? (and print the warning to the console/log file)
Why I would like to enable warnings: For example if I inadvertently re-define a method in a controller, then I would be aware of the problem by looking at the warning printed to the console/log file. See here for an example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其放在初始化代码中的某个位置(例如
config/application.rb
):不过,您可能还会从 Rails 本身收到一些警告。
Put this somewhere in your initialisation code (such as
config/application.rb
):You'll probably also get some warnings from Rails itself though.