Pry Tips

Pry is a power­ful tool for de­bug­ging ruby codes. It’s idea is bril­liantly awe­some. It’s source code is easy to read and un­der­stand. Pry is already an ir­re­place­able part of my dev. env.. Below I am going to share a bit how I tuned it, hope that is also in­ter­est­ing to you. This one is quite tricky so be­fore mov­ing on, please try Pry a while if you haven’t used it be­fore.

Focus when Entering Session

Pry al­lows us to add hooks, block below will be ex­ecuted every time en­ter­ing Pry ses­sion. Add it into your ~/.pryrc and it will ask Tmux to se­lect the win­dow run­ning Pry, and bring the ter­minal on top with wmc­trl.

1
2
3
4
5
6
7
8
Pry.config.hooks.add_hook(:before_session, :focus) do
  _pane = ENV['TMUX_PANE']
  if _pane
    _wid = `tmux list-window -t main -F '\#{window_index} \#{window_id}' | grep '@#{ _pane[1..-1] }$'`.split(' ')[0]
    `tmux select-window -t main:#{_wid}`
  end
  `wmctrl -a "Main Tmux Session"`
end

As you can see, this is very spe­cific for my env. (as­sum­ing a tmux has ses­sion named ‘main’ and is run­ning under ter­minal with spe­cific title). Please feel free to modify it to fit you.

Source Code Editing

In a Pry ses­sion, you can dy­nam­ic­ally modify source codes with the edit com­mand. Pry al­lows us to con­fig­ure which ed­itor to use. Do you re­mem­ber how I open file in pro­ject-based vim using a cus­tom launcher? We can apply it in Pry too.

1
2
3
Pry.config.editor = proc { |file, line|
  "xdg-open 'vim://#{file}:#{line}'"
}

Trigger Code Reload

After modi­fy­ing a source file, we will want Pry to re­load dy­nam­ic­ally without re­start­ing our ap­plic­a­tion. The Pry.load method come in handy.

1
2
3
4
5
6
7
8
" call pry to reload the current file
" example: Pry.load "/home/loki/projects/loper/app.rb"
function! ReloadCurrentFileInPry()
  let path = expand("%:p")
  let sendcmd = "send-keys -t main '" . 'Pry.load "' . path . '"' . "' Enter"
  call system("tmux " . sendcmd)
endfunction
nnoremap <leader>pp :call ReloadCurrentFileInPry()<cr>

Now whenever you press <leader>pp in VIM, a Pry.​load com­mand will be sent to the cur­rent Tmux main ses­sion.

Plugins

There are 2 must-have plu­gins to me. Check their git­hub page for de­tails.

  1. pry-stack­_­ex­plorer

    Power­ful tool to travel in­side call stacks. When error oc­curred, show-stack re­turns the de­tailed call stacks. Loc­ate the pos­sible method caused prob­lem then up <#id> to go into the con­text and debug.

  2. pry-clip­board

    Copy­ing com­mands into sys­tem clip­board, later on we can paste into our source codes.

Comments