pixelearth
pixelearth

Reputation: 14630

How to run arbitrary rails code when binding.pry is called

Is there a way to run code just when binding.pry is called? I want to do ActiveRecord::Base.logger = Logger.new(STDOUT) to see sql queries.

I want this to run every time binding.pry is used, not just manually once.

Upvotes: 0

Views: 165

Answers (1)

oystersauce8
oystersauce8

Reputation: 551

A pry hook can be executed just before landing on the pry prompt

Pry.hooks.add_hook(:before_session, "my_hook") do |output, binding, pry|
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

The pry folks have documented this well at github.com/pry/pry/wiki/Hooks

Upvotes: 1

Related Questions