简体   繁体   English

为什么Rails控制台不会加载对ruby文件的更改

[英]Why will Rails Console not load Changes to ruby file

I am writing a script that I run in the rails console. 我正在编写一个我在rails控制台中运行的脚本。

When I make a change to the file, even if I exit and reload the rails console, it does not recognize these changes. 当我对文件进行更改时,即使我退出并重新加载rails控制台,它也无法识别这些更改。

How do I make rails recognize and load changes to this file? 如何让rails识别并加载对此文件的更改?

The file is in the root of my application. 该文件位于我的应用程序的根目录中。 I load the rails console and run: 我加载rails控制台并运行:

require 'file.rb'

The file will run fine the first time, but if I make changes to the file they are not loaded. 该文件将在第一次运行正常,但如果我对文件进行更改,则不会加载它们。 I could delete all the code in the file and run the file again and it still runs as it did before the edits. 我可以删除文件中的所有代码并再次运行该文件,它仍然像编辑前一样运行。

If you're making changes to files that are stored within the app/ directory, you can reload these with the reload! 如果您正在对存储在app/目录中的文件进行更改,则可以通过重新加载重新加载这些文件reload! command. 命令。

If you're making changes outside of that, you need to exit and restart your console. 如果您在此之外进行更改,则需要退出并重新启动控制台。

As a note, the .rb extension is almost always omitted in a require call. 注意,在require调用中几乎总是省略.rb扩展名。 Another thing to keep in mind is that require 'file' will look in your $LOAD_PATH for something called file.rb and will load in the first one. 另外要记住的是, require 'file'会在$LOAD_PATH查找名为file.rb ,并将在第一个中加载。 It's possible you have two files with the same name. 您可能有两个具有相同名称的文件。 Try renaming it to see if that has any effect. 尝试重命名它以查看是否有任何影响。

Rails has a pretty good auto-loader so it's usually not necessary to use require if you put your files in the correct place to start with. Rails有一个非常好的自动加载器,所以如果你把文件放在正确的位置开始,通常没有必要使用require For example, MyClass could go in app/models/my_class.rb and would load automatically. 例如, MyClass可以进入app/models/my_class.rb并自动加载。 The autoloader can be configured to look in different places as well. 自动装带器也可以配置为查看不同的位置。

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

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