bachposer
bachposer

Reputation: 973

How to suppress some output at Rails console

Once I do a rails c and get some rows form the db like so:

users = User.find(:all,:conditions => ["some conds"])

then users has say 20 results

If I then do a

users.each do |u|
  puts u.name if u.gender == 'male'
end

then after printing all the male names, the console again outputs all the content of the users object. I don't need to see it again. How can I suppress it? All I am interested in is the output of puts

Upvotes: 3

Views: 2129

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

That's how console works. You enter expression, it prints its value.

users.each do |u|
  puts u.name if u.gender == 'male'
end

Value of this code is users object itself and it is correctly printed. What you print with puts is a side effect.

You can still suppress printing full contents of users by changing return value of this expression. For example, like this:

users.each do |u|
  puts u.name if u.gender == 'male'
end && false

Upvotes: 20

Related Questions