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.
suppresing output to console with ruby
I use the following snippet in tests to capture and test STDOUT
def capture_stdout(&block)
original_stdout = $stdout
$stdout = fake = StringIO.new
begin
yield
ensure
$stdout = original_stdout
end
fake.string
end
With this method, the above would become:
def executing_a_signal
capture_stdout { a_method(a_signal.new, a_model, a_helper) }
assert_equal(new_state, a_model.state)
end
Suppressing stubborn console output on system calls in Ruby
Question was answered in the comments. I moved to Open3.capture3 using a format like below and the console text from webp is captured.
stdout, stderr, status = Open3.capture3("convert -flatten \"#{$temp_dir}#{tmp_image_filename}\" \"#{$temp_dir}#{@image_filename}\"")
Avoid printing after executing command in console
I assume you are doing this in the console. I usually add just "; :ok" if I don't want to see the output.
data = YAML.load_file( ... ) ; :ok
Suppress console output during RSpec tests
I suppress puts
output in my classes by redirecting $stout
to a text file. That way, if I need to see the output for any reason, it is there but it doesn't muddy up my test results.
#spec_helper.rb
RSpec.configure do |config|
config.before(:all, &:silence_output)
config.after(:all, &:enable_output)
end
public
# Redirects stderr and stout to /dev/null.txt
def silence_output
# Store the original stderr and stdout in order to restore them later
@original_stderr = $stderr
@original_stdout = $stdout
# Redirect stderr and stdout
$stderr = File.new(File.join(File.dirname(__FILE__), 'dev', 'null.txt'), 'w')
$stdout = File.new(File.join(File.dirname(__FILE__), 'dev', 'null.txt'), 'w')
end
# Replace stderr and stdout so anything else is output correctly
def enable_output
$stderr = @original_stderr
$stdout = @original_stdout
@original_stderr = nil
@original_stdout = nil
end
EDIT:
In response to the comment by @MyronMarston, it probably would be smarter to just insert the methods directly into before
and after
as blocks.
#spec_helper.rb
RSpec.configure do |config|
original_stderr = $stderr
original_stdout = $stdout
config.before(:all) do
# Redirect stderr and stdout
$stderr = File.new(File.join(File.dirname(__FILE__), 'dev', 'null.txt'), 'w')
$stdout = File.new(File.join(File.dirname(__FILE__), 'dev', 'null.txt'), 'w')
end
config.after(:all) do
$stderr = original_stderr
$stdout = original_stdout
end
end
It looks a little cleaner and keeps methods off of main
.
Also, note that if you are using Ruby 2.0, you can use __dir__
instead of File.dirname(__FILE__)
.
EDIT2
Also it should be mentioned, that you can forward to true os /dev/null
by using File::NULL
as it was introduced in Ruby v 1.9.3. (jruby 1.7)
Then the code snippet will look as following:
#spec_helper.rb
RSpec.configure do |config|
original_stderr = $stderr
original_stdout = $stdout
config.before(:all) do
# Redirect stderr and stdout
$stderr = File.open(File::NULL, "w")
$stdout = File.open(File::NULL, "w")
end
config.after(:all) do
$stderr = original_stderr
$stdout = original_stdout
end
end
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
Related Topics
Phonegap Mobile Rails Authentication (Devise? Authentication from Scratch)
Write Array of Radix-2 Numeric Strings to Binary File in Ruby
Ruby Errors with Os X Yosemite
Instance Variable, Class Variable and the Difference Between Them in Ruby
(New to Rails) Rails Server Error: Cannot Load Such File -- Bootsnap/Setup (Loaderror)
Is Subclassing a User Model Really Bad to Do in Rails
Sign Out Specific User with Devise in Rails
Log Doesn't Work in Production with Delayed Job
How to Convert a Scientific Notation String to Decimal Notation
Paperclip File Not Found Error
Sort a List of Objects by Using Their Attributes in Ruby
Multi Level Block Method Is Generating Issue