简体   繁体   English

Rails控制台:多次运行Ruby文件

[英]Rails console: Run a Ruby file several times

Rails provides a very useful console ('script/console'). Rails提供了一个非常有用的控制台('script / console')。

I write a ruby file and run it in the console using require foo.rb . 我编写了一个ruby文件,并使用require foo.rb在控制台中运行它。

It works fine the first time, but the second and next require foo.rb does not run my script ( require does not reload it). 它第一次工作正常,但第二次和下一次require foo.rb不运行我的脚本( require不重新加载它)。

Do you have any tips/tricks? 你有什么提示/技巧吗?

require is used to load extensions - so the code will execute once, to get the extensions to be present in your environment, but subsequent requires won't do anything, because the job has already been done. require用于加载扩展 - 因此代码将执行一次,以使扩展在您的环境中出现,但后续要求将不会执行任何操作,因为作业已经完成。

load , on the other hand, loads and executes the code every time. 加载 ,而另一方面,加载并执行代码每次。

As already mentioned, if you just want to run your script and you need the Rails environment, then consider using script/runner 如前所述,如果您只想运行脚本并且需要Rails环境,那么请考虑使用script / runner

You should probably try either loading your rails environment in a script or using rake. 您应该尝试在脚本中加载rails环境或使用rake。 Also consider using script/runner. 还要考虑使用script / runner。

Here is an old and possibly outdated example of using your rails environment in a script. 以下是在脚本中使用rails环境的旧的且可能过时的示例。 A more recent and detailed version here . 一个更近的和详细的版本在这里

A stack overflow answer 堆栈溢出答案

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

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