ruby-on-railsirbruby-on-rails-7

Rails console overriding `~/.irbrc`


I have the following in my ~/.irbrc file:

require 'irb'
require 'rubygems'
require 'awesome_print'

AwesomePrint.irb!

IRB.conf[:USE_AUTOCOMPLETE] = false
IRB.conf[:ECHO_ON_ASSIGNMENT] = true
IRB.conf[:USE_MULTILINE] = false

# Add color coding based on Rails environment for safety

if defined? Rails
  banner = if Rails.env.production?
             "\e[41;97;1m #{Rails.env} \e[0m "
           else
             "\e[42;97;1m #{Rails.env} \e[0m "
           end

  # Build a custom prompt
  IRB.conf[:PROMPT][:CUSTOM] = IRB.conf[:PROMPT][:DEFAULT].merge(
    PROMPT_I: banner + IRB.conf[:PROMPT][:DEFAULT][:PROMPT_I]
  )

  # Use custom prompt by default
  IRB.conf[:PROMPT_MODE] = :CUSTOM
end

In my Rails app directory, if I run irb then the file is sourced as shown by the disabled :USE_AUTOCOMPLETE = false: enter image description here

However when I run rails c, then the file does not appear to be sourced; :USE_AUTOCOMPLETE is enabled: enter image description here

Is this a load path issue or something like that?

UPDATE It seems it is sourcing the file but is perhaps being overridden? When I insert a deliberate error in the ~/.irbrc file and execute rails c I get an error:

❯ rails c
Loading development environment (Rails 7.0.3.1)
/Users/johnpitchko/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/irb-1.4.1/lib/irb/init.rb:336:in `load': /Users/johnpitchko/.irbrc:29: syntax error, unexpected end-of-input (SyntaxError)

Solution

  • Turns out it was the require awesome_print line. I disabled that and AwesomePrint.irb! and it worked fine.

    I had the amazing_print gem in my Gemfile.