NO LOS IGNORES!

Son warnings emitidos internamente por Ruby, NO son lo mismo que los Deprecation Warnings de Rails.

Meme epic handshake

Se pueden emitir warnings en cualquier momento usando el método `warn` del Kernel.

              
  def warn(*msgs, uplevel: nil, category: nil)
              
            

Por qué queremos a los warnings? ❤️

Meme de Boromir diciendo One Does Not Simply Change The Ruby Version

Categorías

  • :deprecated
  • :experimental
  • :performance
  • otros sin categoría?

Cómo controlar los warnings

MEME perro usando computadora

Flag con el comando `ruby`

  • `ruby -W loquesea.rb` o `ruby -W0` o `ruby -W2:deprecated`
  • `ruby -W2:no-deprecated`
  • Niveles: 0 (silenciadas), 1 (algunas) o 2 (verbose, default pasando `-W`)
  • `ruby -w loquesea.rb` (equivalente a `-W0`)
  • `ruby -d loquesea.rb` o `ruby --debug loquesea.rb` (equivalente a `-W2`)

Programáticamente

  • `Warning[:deprecated] = true`
  • `$VERBOSE = true`
  • Otras variables globales: `$-v` `$-w` `$-W` `$DEBUG` `$-d`

La variable de entorno RUBYOPT

  • Sirve para setear los warnings cuando no ejecutamos ruby/irb directamente.
  • Soporta los mimos flags: `RUBYOPT="-W2" rails test`.

Qué warnings se muestran?

Redefinición de métodos y constantes

/usr/share/rvm/gems/ruby-3.1.3/gems/titleize-1.4.1/lib/titleize.rb:124: warning: method redefined; discarding old titleize /usr/share/rvm/gems/ruby-3.1.3/gems/activesupport-7.0.4.2/lib/active_support/inflector/methods.rb:182: warning: previous definition of titleize was here

Referencia circular en requires

/usr/share/rvm/gems/ruby-3.1.3/gems/webdrivers-5.0.0/lib/webdrivers/railtie.rb:3: warning: loading in progress, circular require considered harmful - /usr/share/rvm/gems/ruby-3.1.3/gems/webdrivers-5.0.0/lib/webdrivers.rb # stacktrace

Variables sin usar

/usr/share/rvm/gems/ruby-3.1.3/gems/sidekiq-cron-1.9.1/lib/sidekiq/cron/job.rb:318: warning: assigned but unused variable - e /usr/share/rvm/gems/ruby-3.1.3/gems/flipper-0.26.0/lib/flipper/middleware/memoizer.rb:56: warning: assigned but unused variable - reset_on_body_close

Código que no se puede ejecutar

/usr/share/rvm/gems/ruby-3.1.3/gems/mail-2.8.1/lib/mail/parsers/date_time_parser.rb:837: warning: statement not reached Ejemplo

`elsif` sin condición

/usr/share/rvm/gems/ruby-2.7.7/gems/rexml-3.1.7.3/lib/rexml/text.rb:112: warning: `elsif' at the end of line without an expression

Indentación incorrecta

/usr/share/rvm/gems/ruby-2.7.7/gems/custom-hubspot-0.1.1/app/services/hubspot_service.rb:128: warning: mismatched indentations at 'end' with 'class' at 3

Shadowing de variable dentro de bloque

/usr/share/rvm/gems/ruby-2.5.8@pecas/gems/tzinfo-1.2.9/lib/tzinfo/zoneinfo_timezone_info.rb:506: warning: shadowing outer local variable - i

Esto lo sacaron de Ruby 2.6 ...

Assignment en condicional

/path/to/some/file.rb:15: warning: found `= literal' in conditional, should be ==

Código que afecta negativamente la performance

./test.rb:4: warning: Redefining 'String#freeze' disable multiple interpreter and JIT optimizations

Features experimentales

./test.rb:4: warning: Ruby::Box is experimental, and the behavior may change in the future! ./test.rb:4: warning: Ractor API is experimental and may change in future versions of Ruby.

Deprecations

O "deprecaciones"? según wikitionary está bien decir deprecado

keyword arguments

/usr/share/rvm/gems/ruby-2.7.7/gems/actionpack-5.2.4.4/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /usr/share/rvm/gems/ruby-2.7.7/gems/activemodel-5.2.4.4/lib/active_model/type/integer.rb:13: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call Link

frozen string literal

/some/path/to/ruby/file.rb:123: warning: literal string will be frozen in the future (run with --debug-frozen-string-literal for more information)

Este warning es distinto al resto, porque nos dice dónde se modificó el string pero no cómo corregirlo

frozen string literal (cont)

`RUBYOPT="-W:deprecated --debug=frozen-string-literal" ...`

/some/path/to/ruby/file.rb:123: warning: literal string will be frozen in the future (run with --debug-frozen-string-literal for more information) /another/ruby/file.rb:567: info: the string was created here

Constantes y métodos que se van a remover

/usr/share/rvm/gems/ruby-2.5.8/gems/sass-3.2.19/lib/sass/version.rb:115: warning: File.exists? is a deprecated name, use File.exist? instead /usr/share/rvm/gems/ruby-2.7.7/gems/bundler-1.17.3/lib/bundler/shared_helpers.rb:29: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2. /usr/share/rvm/gems/ruby-2.7.7/gems/bundler-1.17.3/lib/bundler/rubygems_integration.rb:200: warning: constant Gem::ConfigMap is deprecated

Warnings y Rails

  • Rails configura programáticamente los warning de deprecations y nivel 2
    Link a github
  • No es del todo confiable, mucho código se carga antes que el código de Rails
  • Sólo activa los warnings de deprecations, pero no las otras categorías
  • Usar `RUBYOPT` es la mejor forma de ver realmente todos los warnings de Ruby

Gracias! preguntas?


arielj.github.io/ruby-warnings