Copying Rails console output to clipboard on Mac

August 2017

From time to time I want to work with information which is from rails console output outside of the command line. Helpfully, ruby provides a neat way to pipe console output into the mac system clipboard using IO:

IO.popen("pbcopy", "w") do |pipe| 
  # Get users without subscriptions as CSV and add them to the clipboard
  pipe.puts User.includes(:subscription).where(subscriptions: {user_id: nil}).pluck(:email).to_csv
end

Ever wish you got more email?

Neither do I. You're busy, and so is your inbox. I'll only be in touch when I publish something new. And of course it goes without saying your email will be kept completely private.