A bit verbose, are we?
While “debugging” a Rails application, developers tend to use the puts method to print debug information while developing. Although perhaps useful for the developer in mind, it makes it a lot harder for colleagues to use testing in their work as the console is cluttered with debug information.
Without going into whether using puts for debugging is a good practice or not, I wrote some Ruby code today that silences all this information while I’m running my tests. A very simple analysis of the problem tells me that:
- I only want the output silenced when running my tests. I found it tempting to force my policy of not using puts on my peers, but strictly spoken all I wanted was my tests to run less verbosely
- The reason for all this turning up in the console is the use of the method puts
The first point tells me that testhelper.rb is the natural place to put this functionality. This file is always loaded when you run your tests. The second point tells me that the method I should target is the puts method – and the puts calls are made in ActiveRecord::Base subclasses. So my first try was to put the following into testhelper.rb:
module ActiveRecord
class Base
def puts(something)
# let's eat it
end
end
end
Running my tests, I was pleased to see about 50% of the output silently eaten by this hack. However, there was still some output sent from classes not extending ActiveRecord::Base, so I needed to be a bit more greedy. Let’s put it all into a module:
module SilentOutput
def puts(something)
#
end
end
module ActiveRecord
class Base
include SilentOutput
end
end
class SomeOtherClass;include SilentOutput;end
There! Now I can get back to work. One last issue before I let you get back to work: How do you discover which classes called the puts method, so you know which ones to include? A small trick is to put the following into your test helper file, and it will reveal other classes calling the method:
Kernel.module_eval do
alias_method :old_puts, :puts
def puts(something)
raise "WARNING: #{self.class.to_s} tried to call puts with the message #{something}"
end
end
These lines mix into Kernel and creates an alias to the (old) puts method – in case you need it actually puts-ing, and then raises an error revealing which class tried to use puts to print its output. This gives you a hint of which further classes may be candidates for including your SilentOutput module.

Such a superbly written post.. Thnkx after sharing this post! <IMG>http://www.sedonarapidweightloss.com/weightloss-diet/34/b/happy.gif</IMG>
av Anonym, 15/12-2010
Na Leute, Nachdem ich in in den letzten Tagen bei mehreren Communities nachgeschaut habe, kam ich in Folge der positiven Ansichten die da beschrieben werden, dazu mich bei http://www.homepages-eintragen-suchmaschine.de eintragen zu lassen. Meine Webseite, wurde dann nach einiger Zeit bei google und noch weiteren Suchmaschinen angemeldet. Mir hat es aufjedenfall viel gebracht ! Als meine Internetseite bei den Suchmaschinen angemeldet wurde, ist meine Besucheranzahl stetig besser geworden. Daher kann Ich euch eine Anmeldung mit guten Gewissen ans Herz legen. K?nnt es ja mal testen Bis bald. Eure, Valeria
av sminnaopponse, 23/12-2010
Hostspage.com Premium Web hosting offers you and your customers topnotch features. We have 3 low priced plans and all are unmetered. All plans are activated instantly.
Use the coupon 25off at checkout for lifetime 25% off
Unmetered Shared Plan $3.99 month before discount
Click here to order now : <a href=http://www.hostspage.com/cart.php?a=add&pid=2>http://www.hostspage.com/cart.php?a=add&pid=2</a>
Unmetered Reseller Plan $5.99 mo before discount
Click here to order now : <a href=http://www.hostspage.com/cart.php?a=add&pid=238>http://www.hostspage.com/cart.php?a=add&pid=238</a>
Unmetered Master Reseller $7.99 mo before discount
Click here to order now : <a href=http://www.hostspage.com/cart.php?a=add&pid=239>http://www.hostspage.com/cart.php?a=add&pid=239</a>
Our cPanel servers are bundled with more software and add-ons than anyone else in the industry! Our secure and optimized cPanel/WHM software is just the beginning: Fantastico, Softaculous, WHMXtra, RVSkin, RVSiteBuilder PRO, ZamFoo Master Reseller, R1Soft’s and Installatron continuous data protection backups are all built right into your cPanel web interface for both you and your customers to enjoy!
All plans include : Unlimited Subdomains Unlimited Parked Domains Unlimited MySQL 5 Databases Unlimited Email Accounts Unlimited Auto Responders Unlimited Forwarders Unlimited Mail Lists POP3 Access IMAP Access SMTP Access SPAM Assassin Server Spam Filters Virus Protection Automated Web Site Builder
Over 185 One click Scripts in Softaculous
R1Soft Continuous Data Protection
The most advanced data protection software available for your ’ data. We include fully automated back-ups to a remote offsite storage system, along with the cPanel R1Soft web interface module, so your customers can restore their own data at any time without assistance.
All servers are hosted on Dell R410 Dual Xeon e5504 (8 Cores) w/16 GB RAM + RAID-10 disk system, on a Juniper- and Cisco-powered, Tier 1/BGP4 redundant premium network
US based Super responsive and friendly support.
99.99% SLA uptime guarantee.
30 Days Money Back Guarantee (Limit one refund per customer)
Premium stable hosting done right – we guarantee it.
<a href=http://www.hostspage.com/submitticket.php?step=2&deptid=1>http://www.hostspage.com/submitticket.php?step=2&deptid=1</a>
av AgewowekRop, 19/02-2012
Drop in on us at times to
obtain more knowledge and facts
at all events Visit us
contemporary to buy more
knowledge and facts regarding
<a href=http://www.kreatyny.com.pl/tag/creatine>Kreatyna</a>
av Etesishinuell, 25/02-2012