In my .pryrc (part 1 of… 3?)

I develop software all day, every day. Some days, I don’t open my editor, and it’s all drawings on a whiteboard. Other days, I write five little classes that work together to solve a big problem. And sometimes, I write code to help me figure out what I’m doing. Some of that code goes into the .pryrc of the current project, so that every teammate can use it and so that it’s available on production servers as well. In this mini-series of who-knows-how many parts, I’m going to talk about some of that code. Perhaps it can be useful for others, and perhaps it can inspire better tools.

Let’s start with something simple:

def tacit(&block)
  if block_given?
    Rails.logger.silence(::Logger::WARN, &block)
  else
    if Rails.logger.level != ::Logger::DEBUG
      puts "Rails.logger.level = ::Logger::DEBUG"
      Rails.logger.level = ::Logger::DEBUG
    else
      puts "Rails.logger.level = ::Logger::INFO"
      Rails.logger.level = ::Logger::INFO
    end
  end
end

It is almost too simple to mention, but it pulls its weight: this function allows me to silence the logger when running one-off hacks that query the database a lot while spitting out data. Use it like so:

tacit { Merchant.each { |m| m.orders.each { |o| puts "…" if … }}.flatten};

Quick notes:

  • The semicolon at the end?  It’s the neatest trick pry ever gave us: it suppresses output of the last result.
  • I use each here so that the example will fit in one line. Always use find_each in real life, especially if you are extracting info from your production data.

I can also run tacit alone to silence output for a while and then run it again to turn on logging again.

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *