8

Possible Duplicate:
Rails console, how to stop output of return value?

Consider this session in IRB:

>> for a in 1..5 do
?>     puts a
>> end
1
2
3
4
5
=> 1..5
>> 

How do I suppress the output => 1..5? This is important if I do this in a Rails console session:

for post in Post.find(:all) do
    if post.submit_time > Time.now
        puts "Corrupted post #{post.id} is from the future"
    end
end

I don't want all the Posts to be printed as an array at the end. How do I suppress that output?

I am sure there are other ways of doing this, like find_each or a Ruby script but I am more interested in doing this in an interactive session.

Community
  • 1
  • 1
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
  • See similar questions [here](http://stackoverflow.com/questions/10150541/rails-console-how-to-stop-output-of-return-value), [here](http://stackoverflow.com/questions/4678732/how-to-suppress-rails-console-irb-outputs), and a blog post [here](http://austinruby.com/2006/10/6/quieting-irb-s-return-value) – pjumble Jul 23 '12 at 21:51

2 Answers2

28

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 
Kristján
  • 18,165
  • 5
  • 50
  • 62
6

This is core IRB functionality. You type an expression in, it prints its value. In this case, the value is 1..5. Other output is just a side effect.

You can, however, "minimize" returned (and printed) value. So, instead of big array of fat AR models, you can return something small.

Try something like this:

% irb
1.9.3p194 :001 > for a in 1..5 do
1.9.3p194 :002 >     puts a
1.9.3p194 :003?>   end; nil
1
2
3
4
5
 => nil 
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367