Last active
August 29, 2015 14:28
-
-
Save egwspiti/c11278101f39759a9962 to your computer and use it in GitHub Desktop.
preserve local variables across 'cd' in pry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pry::Command::Cd | |
def _binding | |
_pry_.binding_stack.last | |
end | |
def save_local_variables(key, b) | |
state.locals ||= {} | |
builtin_locals = [:__, :_, :_dir_, :_file_, :_ex_, :_pry_, :_out_, :_in_] | |
_local_variables = b.eval('local_variables') | |
state.locals[key] = (_local_variables - builtin_locals).map do |var| | |
[var, b.local_variable_get("#{var}")] | |
end.to_h | |
end | |
def restore_local_variables(key, b) | |
state.locals[key] && state.locals[key].each do |var, val| | |
_pry_.inject_local("#{var}", val, b) | |
end | |
end | |
end | |
Pry.hooks.add_hook(:before_cd, 'before_cd_hook') do | |
save_local_variables _binding.eval('self'), _binding | |
end | |
Pry.hooks.add_hook(:after_cd, 'after_cd_hook') do | |
restore_local_variables _binding.eval('self'), _binding | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment