How to Suppress the Output of Return Value in Irb/Rails Console

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

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.

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.

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

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.

Rails console limits text output. How do I get to see it all?

I don't think irb or the Rails console is the problem. This doesn't truncate from either plain irb or rails c:

(1..100000).reduce(''){|a,i| a << i.to_s}

The Rails console output is coming from the inspect method, so it's probably ActiveRecord that is doing the truncation (code here). You should be able to override the inspect method in your User model if you want custom output.

How Do You Clear The IRB Console?

On Mac OS X or Linux you can use Ctrl + L to clear the IRB screen.

Suppressing the output of a command run using 'system' method while running it in a ruby script

After a call to system the exit code is in the special variable $? so if useradd returns different values to indicate if the user was successfully added (e.g. 0 for success) then you can do the following:

system('useradd xx > /dev/null')
if $? == 0
puts 'added'
else
puts 'failed'
end

where the redirect to /dev/null will suppress the output.

Alternatively if the program being called does not use its exit code to indicate success or failure you can use backticks and search for a particular substring in the output e.g.

if `useradd xx`.include? 'success'
puts 'it worked'
else
puts 'failed to add user'
end


Related Topics



Leave a reply



Submit