Cómo suprimir las salidas de consola/irb de Rails


Estoy atascado con un problema bastante raro.

Estaba probando algunas entradas de bd en nuestro servidor de producción en la consola Rails donde casi todos los comandos estaban dando como resultado un gran número de líneas o / p, debido a lo cual el canal ssh se estaba colgando: (

¿Hay alguna forma de suprimir las pantallas de console/irb?

Gracias

Author: ghtn, 2011-01-13

6 answers

Puede añadir ; nil a todos sus sus órdenes / declaraciones.

Ejemplo:

users = User.all; nil

En realidad irb imprime el valor (devuelto) de la última sentencia ejecutada. Por lo tanto, en este caso solo imprimirá nil, ya que nil es la última sentencia válida ejecutada:)

 166
Author: intellidiot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-13 10:04:04

En busca de una solución para silenciar la salida irb/console, también encontré una respuesta en austinruby.com :

Silencio irb:

conf.return_format = ""

Salida predeterminada:

conf.return_format = "=> %s\n"

Límite a eg 512 caracteres:

conf.return_format = "=> limited output\n %.512s\n"
 29
Author: LarsDK,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-15 21:14:18

Aquí, agrega esto a tu~/.irbrc:

require 'ctx'
require 'awesome_print'

module IRB
  class Irb    
    ctx :ap do
      def output_value()
        ap(@context.last_value)
      end
    end
    ctx :puts do
      def output_value()
        puts(@context.last_value)
      end
    end
    ctx :p do
      def output_value()
        p(@context.last_value)
      end
    end
    ctx :quiet do
      def output_value()
      end
    end
  end
end

def irb_mode(mode)
  ctx(mode) { irb }
end

(Nota: Primero debe instalar la gema ctx, aunque awesome_print es opcional, por supuesto.)

Ahora, cuando esté en cualquier consola que use irb, puede hacer lo siguiente:

Modo normal:

irb(main):001:0> { this:'is a complex object', that:[ { will:'probably'}, { be:'good to read' } ], in:{ some:{ formatted:'way'} } }

=> {:this=>"is a complex object", :that=>[{:will=>"probably"}, {:be=>"good to read"}], :in=>{:some=>{:formatted=>"way"}}}

...sí, justo lo que esperas.

awesome_print modo:

irb(main):002:0> irb_mode(:ap)
irb#1(main):001:0> { this:'is a complex object', that:[ { will:'probably'}, { be:'good to read' } ], in:{ some:{ formatted:'way'} } }

=> {
    :this => "is a complex object",
    :that => [
        [0] {
            :will => "probably"
        },
        [1] {
            :be => "good to read"
        }
    ],
      :in => {
        :some => {
            :formatted => "way"
        }
    }
}

...wow, ahora todo se está imprimiendo increíblemente! :)

Modo Silencioso:

irb#1(main):002:0> irb_mode(:quiet)
irb#1(main):001:0> { this:'is a complex object', that:[ { will:'probably'}, { be:'good to read' } ], in:{ some:{ formatted:'way'} } }
irb#1(main):002:0>

... whoah, no hay salida en absoluto? Agradable.

De todos modos, puede agregar el modo que desee, y cuando haya terminado con ese modo, simplemente exit fuera o él, y volverá al modo anterior.

Espero que haya sido útil! :)

 9
Author: Mason Cloud,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-08 02:05:37

Suprima La Salida, En General

También, dependiendo de sus necesidades, eche un vistazo a usar quietly o silence_stream para suprimir la salida en general, no solo en el irb/console:

silence_stream(STDOUT) do
  users = User.all
end

NOTA: quietly será obsoleto en Ruby 2.2.0 y eventualmente será eliminado. (Gracias BenMorganIO !)

Puede encontrar más información aquí.

 5
Author: Joshua Pinter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-15 06:49:14

Ir irb simple simple-prompt {noecho

 3
Author: user3490179,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-03 11:44:19

Ejecutar lo siguiente dentro de irb funciona para mí:

irb_context.echo = false
 0
Author: schpet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-12 18:20:23