97

If I, say, loop through all the instances of a given model and output something from each, at the end, irb will still print the entire object.

If the object ends up taking hundreds of lines, it'll be a long way up before I see what I was actually looking for. Is there a way to disable this in the rails console?

dsp_099
  • 5,801
  • 17
  • 72
  • 128

4 Answers4

125

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
aef
  • 4,498
  • 7
  • 26
  • 44
107

Call conf.echo = false and it will not print the return value. This works for any irb session, not just Rails console.

In case you want to make it permanent, add it to your irb config.

echo 'IRB.conf[:ECHO] = false' >> $HOME/.irbrc
mahemoff
  • 44,526
  • 36
  • 160
  • 222
lulalala
  • 17,572
  • 15
  • 110
  • 169
  • What would you do if you wanted to have this always be the case by default? – Peter Berg May 09 '14 at 21:00
  • @Accipheran I guess putting it in the Rails initializers would work. – lulalala May 11 '14 at 01:05
  • Thanks for the response, I actually found that just throwing the line `IRB.conf[:ECHO] = false`, in my .irbrc file took care of it. That of course also turns of the echo in all my irb sessions, but I'm okay with that. – Peter Berg May 12 '14 at 12:49
  • Is there a way to set it temporarily (in just the current console session)? – stevec Jan 24 '21 at 13:12
  • Running `conf.echo = false` from the rails console gives `NameError: undefined local variable or method 'conf' for main:Object from (pry):6:in '
    '`
    – stevec Jan 24 '21 at 13:23
  • 1
    @stevec I wonder if it is because you are using pry. – lulalala Jan 25 '21 at 01:38
  • @lulalala I just checked against an app that has pry-rails gem and one that doesn't, and running `conf.echo = false` in the rails console worked in the app that doesn't have it and didn't work in the that has pry-rails. I wonder if there's an equivalent for apps using pry? – stevec Feb 21 '21 at 13:44
49

To temporarily stop the console from printing the return values you can issue a nil statement at the end of your loop or function, but before pressing the return.

record.each do |r|
  puts r.properties
end; nil

Or it can be a number too, if you want to reduce typing. But it can be confusing in scenarios, which I can't think of.

record.each do |r|
  puts r.properties
end; 0
RajaRaviVarma
  • 2,502
  • 25
  • 26
3

This frustrated me a lot, because I was using pry-rails gem, some solutions wouldn't work for me.

So here's what worked in 2 steps:

  1. Simply adding ; to the end of the very last command will be enough to silence the output from printing.
  2. It may still print the sql that was executed. So to silence that, surround it with
ActiveRecord::Base.logger.silence do
  # code here
end

Example

If you want to run this

User.all do |user|
  puts user.first_name
end

then this will ensure nothing else prints to screen:

ActiveRecord::Base.logger.silence do
  User.all do |user|
    puts user.first_name
  end
end;

(don't forget the ; at the very end)

stevec
  • 41,291
  • 27
  • 223
  • 311