2021-10-24
Improving Rails Console Workflow
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
- 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
- Copy the file to the remote host
scp /tmp/my_patch_name.rb user@remotehost:/tmp/
- 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.
comments powered by Disqus