Skip to main content

custom_pry_rc_for_rails_console

🎛️ Customize Pry for Memory and Dependency Management

Override your console session via ~/.pryrc to inject helpers for GC tuning, memory stats, and automatic dependency clearing between reloads. This setup turns your console into a lightweight profiling and hot‑reload playground.

# ~/.pryrc
if defined?(Rails)
Pry.config.prompt_name = "#{Rails.application.class.module_parent_name}-console"

# GC and memory stats command
Pry::Commands.block_command 'memstats', 'Show GC and heap statistics' do
stats = GC.stat
puts "Heap live slots: #{stats[:heap_live_slots]}"
puts "Total objects allocated: #{stats[:total_allocated_objects]}"
end

# Auto-clear loaded constants for true code reload
Pry.after_session do
ActiveSupport::Dependencies.clear
puts "Cleared loaded classes for fresh reload!"
end
end