Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Ruby warnings in Rails?

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.

like image 565
Zabba Avatar asked May 25 '11 19:05

Zabba


People also ask

How do you silence a ruby warning?

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.

What is Deprication warning?

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.

Is Ruby deprecated?

With the release of Dart Sass 1.0. 0 stable last week, Ruby Sass was officially deprecated.

What is deprecation warning in Python?

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 .


1 Answers

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.

like image 189
molf Avatar answered Oct 24 '22 04:10

molf