Foliosus Web Design LLC: Your website done right

Blogfolio

Change the prompt in the Rails console

Mar 04 ’09

At work, I frequently find myself in the situation of having two or three or more console windows open, working on multiple apps in different environments. After almost getting myself into trouble on several occasions, I decided that I'd rather have the Rails console tell me what environment I'm running in.

Today I did a little bit of googling, and a little bit of code/docs spelunking, and figured out how to change the prompt. Put the following in your .irbrc:

if ENV['RAILS_ENV']
  # Set up the prompt to be slightly more informative
  rails_env = ENV['RAILS_ENV'].downcase[0,3]
  current_app = Dir.pwd.split('/').last
  IRB.conf[:PROMPT].reverse_merge!(:RAILS_ENV => {:PROMPT_I=>"#{current_app} #{rails_env} >> ", :PROMPT_N=>"#{current_app} #{rails_env} >> ", :PROMPT_S=>nil, :PROMPT_C=>"?> ", :RETURN=>"=> %s\n"})
  IRB.conf[:PROMPT_MODE] = :RAILS_ENV
end

Now my console prompt looks like this, in development:

app_name dev >>

And like this in production:

app_name pro >>

You can go hog-wild, with this technique, customizing your prompts to your heart's desire.

And I no longer worry about accidentally dropping all of my production db records.

    Start the conversation

    * indicates a required field

    New comment

    Will never be disclosed or used inappropriately

    Enter the word how to confirm that you are a human being

    About the author

    Brent Miller is the owner and principal web designer of Foliosus Web Design LLC in Portland, Oregon. He enjoys food, plants, and the color green. If you are interested in hiring him for web work, please contact him.

    Categories

    Plant of the day

    Unknown sp. (Bromeliaceae)

    Unknown sp. (Bromeliaceae)

    Fresh photos