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.
The TL;DR is that you need to use RUBYOPT='-W:no-deprecated -W:no-experimental' to disable the deprecations. This will also disable experimental feature warnings as well.
Deprecation warnings are a common thing in our industry. They are warnings that notify us that a specific feature (e.g. a method) will be removed soon (usually in the next minor or major version) and should be replaced with something else.
With the release of Dart Sass 1.0. 0 stable last week, Ruby Sass was officially deprecated.
To warn about deprecation, you need to set Python's builtin DeprecationWarning as category. To let the warning refer to the caller, so you know exactly where you use deprecated code, you have to set stacklevel=2 .
Put this somewhere in your initialisation code (such as config/application.rb
):
$VERBOSE = true
You'll probably also get some warnings from Rails itself though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With