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