How can I detect if my code is running in the console in Rails 3? How can I detect if my code is running in the console in Rails 3? ruby-on-rails ruby-on-rails

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


You could try this perhaps

if defined?(Rails::Console)  # in Rails Consoleelse  # Not in Rails Consoleend


Many years later there is a better method to do this registering blocks to run for the console (using the railtie interface).

So in the initializer you can write:

Rails.application.console do  # your code hereend

The good think about this is that it also works for runner and it should also work with spring (but I didn't test that).