简体   繁体   English

尝试使用Rails 3启动脚本/控制台

[英]Trying to launch script/console using Rails 3

Using Rails 3. In my project directory am trying to launch rails script/console by using "rails console" command line & get this in return. 使用Rails 3.在我的项目目录中,我尝试使用“rails console”命令行启动rails脚本/控制台并获得此回报。 Loading test environment (Rails 3.2.1) irb(main):001:0> Loading test environment (Rails 3.2.1) irb(main):001:0>

I think you may be confused about the rails commands. 我想你可能会对rails命令感到困惑。

rails server (or script/server when using Rails 2.x) is used to start a web server for local development (this by default is Webrick running on 0.0.0.0:3000 ). rails server (或使用Rails 2.x时的script/server )用于启动Web服务器以进行本地开发(默认情况下,这是Webrick在0.0.0.0:3000运行)。 This process runs in the foreground and does not allow for interaction. 此过程在前台运行,不允许交互。 It will log output to STDOUT. 它会将输出记录到STDOUT。

rails console (or script/console when using Rails 2.x) is used to start the interactive ruby shell (irb) with your Rails app and environment ( development by default, test in your case) loaded. rails console (或使用Rails 2.x时的script/console )用于启动交互式ruby shell(irb),其中包含Rails应用程序和环境(默认development ,在您的情况下为test )。 This is an interactive shell meaning that you can type ruby code in here and it will be executed when you hit the return key or when it encounters the end of a block. 这是一个交互式shell,意味着您可以在此处键入ruby代码,当您点击返回键或遇到块结束时它将被执行。 Try this out 试试吧

a = ["b", "a", "r","t"]
a.reverse

Will return 将返回

=> ["t", "r", "a", "b"]

Since this also loads your Rails application, you have access to the classes defined in your application. 由于这也会加载您的Rails应用程序,因此您可以访问应用程序中定义的类。 For example, if you have a Person model defined, you can instantiate a new instance by typing the following into irb 例如,如果您定义了Person模型,则可以通过在irb中键入以下内容来实例化新实例

Person.new

To leave the irb, you can type exit to return to your operating system's shell. 要离开irb,可以键入exit返回到操作系统的shell。 I hope this helps to clear up some of the confusion. 我希望这有助于消除一些混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM