Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if my code is running in the console in Rails 3?

I have this code in an initializer:

if $0 == 'irb'   # ... end 

It works fine with Rails 2.3 but in Rails 3 the value of $0 is 'script/rails' no matter if it was started with rails c or rails s. ARGV is an empty array. How can I detect if the application has been started with "rails c" or "rails console"?

like image 909
Markus Avatar asked Jan 05 '11 11:01

Markus


People also ask

How do I get to rails console?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .

What happens when we run rails server?

One of the things rails server does is that it loads all the dependencies/gems required by your Rails app, or at least sets them up to be auto-loaded later when they are needed. This is sometimes called "booting" or loading the "Rails environment".


1 Answers

You could try this perhaps

if defined?(Rails::Console)   # in Rails Console else   # Not in Rails Console end 
like image 113
Aditya Sanghi Avatar answered Oct 05 '22 22:10

Aditya Sanghi