如何检测我的代码是否在Rails 3中的控制台中运行? [英] How can I detect if my code is running in the console in Rails 3?

查看:117
本文介绍了如何检测我的代码是否在Rails 3中的控制台中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在初始化程序中有此代码:

I have this code in an initializer:

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

它适用于Rails 2.3,但在Rails 3中$ 0的值是'脚本/ rails,无论是否使用rails c或rails s启动。 ARGV是一个空数组。如何检测应用程序是否已使用rails c或rails console启动?

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"?

推荐答案

也许

if defined?(Rails::Console)
  # in Rails Console
else
  # Not in Rails Console
end

这篇关于如何检测我的代码是否在Rails 3中的控制台中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆