Sometimes you need to run rails console on a remote machine and override methods for the duration of the session.

Pain points:

  • input lag
  • difficult to write multi-line code
  • hard to edit previously executed multi-line code

You could write the code locally in your favorite editor, copy and paste it into the console. But this can cause issues with irb or pry command interpretation.

Solution

  1. Create a local file /tmp/my_patch_name.rb
class MyClassWithBug
  # alias the original method
  alias old_my_method my_method unless defined? old_my_method

  # patched method
  def my_method
    # fixed code
  end
end
  1. Copy the file to the remote host
scp /tmp/my_patch_name.rb user@remotehost:/tmp/
  1. Run in the console
def load_patch; Kernel.load('/tmp/my_patch_name.rb'); end

load_patch

# test it
MyClassWithBug.new.my_method

Done!

When you need to edit and reapply, just update the file and call load_patch again.

As a bonus, you no longer have multi-line code cluttering your console history.