Building off my previous ~/.pryrc I wanted to automatically load up my core project Ruby file, spec_helper.rb, and fire off some initialization routines whenever I start a new Pry session in my project's directory.
Because the contents of the local directory's .pryrc is evaluated before the :beforesession hook from ~/.pryrc timing is a bit more delicate. I get around this by creating a custom function named prybeforesession (but you could name it anything you want really) and have the ~/.pryrc's before_session hook execute it if it exists.
So my project's .pryrc:
#~/Projects/ActiveAvro/.pryrc def _pry_before_session require 'active_avro' require 'spec_helper' ActiveAvroHelper.initialize end
And my updated ~/.pryrc looks like this:
#~/.pryrc
require 'interactive_editor'
Pry.config.editor = "mate"
# add the current directories /lib and /spec directories to the path if they exist
before_session = Proc.new do |out, target, _pry_|
dir = `pwd`.chomp
%w(lib spec test).map{ |d| "#{dir}/#{d}" }.each { |p| $: << p unless !Dir.exists?(p) || $:.include?(p) }
# if a local .pryrc defines a _pry_before_session function, execute it now
send(:_pry_before_session) rescue nil
end
Pry.hooks[:before_session] = before_session