How to Suppress Rails Console/Irb Outputs

How to suppress Rails console/irb outputs

You can append ; nil to your statements.

Example:

users = User.all; nil

irb prints the return value of the last executed statement; thus in this case it'll print only nil since nil is the last executed valid statement.

How to suppress the output of return value in IRB/Rails Console?

If you just want to suppress long output once in a while, use ;0, like:

a = [*1..10000];0
# => 0

If you want to suppress it generally, use the ~/.irbrc file. The IRB.conf[:INSPECT_MODE] and IRB.conf[:PROMPT][your_prompt][:RETURN] control what is returned. You can figure out what your_prompt is by checking IRB.conf[:PROMPT_MODE]

Example:

IRB.conf[:PROMPT][:DEFAULT][:RETURN] = "" # suppress return value completely

You'll need to restart irb after changing the value.

Hope that helps.

suppressing IRB output?

Just add nil to the end of your command. It doesn't kill irb's response line, but if you have some large object, it avoids blasting your screen.

1.9.3p194 :036 > for a in 1..5 do; puts a; end; nil
1
2
3
4
5
=> nil

Stop rails console from printing out the object at the end of a loop

If you don't want to disable the echo in general you could also call multiple expressions in one command line. Only the last expression's output will be displayed.

big_result(input); 0

Suppress output of Enumerable::each in irb

That is simple.

Just add a semicolon(;) at end of the statement/expression in Ruby whose return value you don't want to see and also add nil.

Make it:

[1,2].each {|u| puts "hey"}; nil

But please note that there is no reason to suppress the irb output(except when the return value is too large/lot of text); it helps a lot in debugging and to know what exactly is going on in the function.

How can I avoid the console output when assigning a value to a variable in Ruby

You can start the irb or console attaching the --noecho option.

$ irb --noecho
2.0.0p353 :001 > true
2.0.0p353 :002 >

Otherwise, if the console was started by another process, simply set conf.echo = false

$ irb 
2.0.0p353 :001 > true
=> true
2.0.0p353 :002 > conf.echo = false
2.0.0p353 :004 > true
2.0.0p353 :005 >

Easy way to suppress SQL output in Rails console?

Enter this in the console, or put it in the console's config file:

ActiveRecord::Base.logger = nil

Prevent ruby/rails from printing results of assignment expressions

Edit:

A version of the MR referenced below was merged to irb, but tweaked a few times since then. So to get the behavior you want it depends on the irb version:

If you have at least 1.2.0, but less than 1.2.6: the default behavior is to not print anything for assignment statements. You have to set IRB.conf[:ECHO_ON_ASSIGNMENT] = true if you want the old behavior (of always printing everything).

If you have 1.2.6, the default value for ECHO_ON_ASSIGNMENT was changed to true, and a new config value: OMIT_ON_ASSIGNMENT was added which will truncate long output for assignment statements, it defaults to true. So you have to set IRB.conf[:ECHO_ON_ASSIGNMENT] = false if you don't want anything to be printed for assignment statements. You have to set IRB.conf[:OMIT_ON_ASSIGNMENT] = false if you want the old behavior (of always printing everything).

If you have 1.2.7 or above, OMIT_ON_ASSIGNMENT was removed in favor of letting ECHO_ON_ASSIGNMENT accept true, false, or :truncate, with the default value being :truncate. A value of true will print everything for assignment statements, false will print nothing, and :truncate will print a truncated version (like setting OMIT_ON_ASSIGNMENT to true in 1.2.6).

Note that the versions above versions of the irb gem, not ruby versions (even though irb is included in ruby's standard library). You can check what version of irb you have by looking at IRB::VERSION. Very old versions of irb don't set this constant, so if it's missing, you definitely have a version lower than 1.2.0.

Original answer:

Until this pull request (or one like it) is merged, you can put the following into your ~/.irbrc file:

require 'ripper'
module IRB
class Irb
ASSIGNMENT_NODE_TYPES = [:assign, :opassign, :massign]
def assignment_expression?(line)
ASSIGNMENT_NODE_TYPES.include?(Ripper.sexp(line)&.dig(1,-1,0))
end

def output_value # :nodoc:
return if assignment_expression?(@context.last_line)
printf @context.return_format, @context.inspect_last_value
end
end

class Context
attr_accessor :last_line
def evaluate(line, line_no, exception: nil) # :nodoc:
@line_no = line_no
@last_line = line
if exception
line = "begin ::Kernel.raise _; rescue _.class; #{line}; end"
@workspace.local_variable_set(:_, exception)
end
set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
end
end
end

Note that since this monkey-patches IRB::Irb and IRB::Context, there's no guarantee that it will be compatible with future releases.



Related Topics



Leave a reply



Submit