如何在Rails应用程序中包含自定义.rb脚本

时间:2021-07-08 18:16:34

Ok, I've got this Ruby script that is supposed to be included in many Rails apps. So I don't want to break it down to pieces and jam it into a particular app, but would rather keep it in 1 piece and have Rails app load it instead. The script would be required mostly from models, mailers and rarely controllers.

好的,我有这个Ruby脚本应该包含在许多Rails应用程序中。所以我不想把它分解成碎片并将它塞进一个特定的应用程序中,但宁愿将它保存在一块中并让Rails应用程序加载它。该脚本主要来自模型,邮件程序和很少的控制器。

So if my script is tools.rb, where in my Rails file tree should I put it and how/where in my Rails app should I include it? Also the script comes with YAML file.

所以,如果我的脚本是tools.rb,我应该把它放在我的Rails文件树中,以及我应该如何/在我的Rails应用程序中包含它?该脚本还附带YAML文件。

Its my second day learning Rails, so please bear with me.

这是我第二天学习Rails,所以请耐心等待。

3 个解决方案

#1


6  

You can keep all the extra stuff in either /app/modules or /lib. And i prefer lib. After putting in the lib folder, require it in any initializer (or create one)

您可以将所有额外的东西保存在/ app / modules或/ lib中。而且我更喜欢lib。放入lib文件夹后,在任何初始化程序中需要它(或创建一个)

require "./lib/tools" in /config/initializers/tools.rb

在/config/initializers/tools.rb中需要“./lib/tools”

And tadaa !! You can use that corresponding class/module anywhere in the rails application !!

和tadaa !!您可以在rails应用程序的任何位置使用相应的类/模块!

And all the YAML files should be put in /config/.

并且所有YAML文件都应该放在/ config /中。

*** fix syntax in '/lib/tools'

***修复'/ lib / tools'中的语法

#2


5  

You can include your own .rb files on lib folder. You can include, modules, class...etc under your own rb files.

您可以在lib文件夹中包含自己的.rb文件。您可以在自己的rb文件下包含,模块,类等。

If you want autoload or autorequire your custom library code, you must open your config/application.rb and add the next line for example:

如果要自动加载或自动查询自定义库代码,则必须打开config / application.rb并添加下一行,例如:

config.autoload_paths += %W(#{config.root}/extras #{config.root}/lib)

You can take a look to:

你可以看看:

http://reefpoints.dockyard.com/ruby/2012/02/14/love-your-lib-directory.html

http://reefpoints.dockyard.com/ruby/2012/02/14/love-your-lib-directory.html

You yaml files should be inside /config/ folder.

你yaml文件应该在/ config /文件夹中。

Regards!

问候!

#3


2  

If you have any module or class defined in script and use that module or class in app you just put it in lib . It will be accessed anywhere in app. Load the file if you need to initialize something which needs your application. Unless needed don't load things. If you think it must be loaded before app starts. Then you can put into config/initializers

如果您在脚本中定义了任何模块或类,并在应用程序中使用该模块或类,则只需将其放在lib中即可。它将在应用程序的任何位置访问。如果需要初始化需要应用程序的内容,请加载文件。除非需要不加载东西。如果您认为必须在应用程序启动之前加载它。然后你可以放入config / initializers

yml files can be loaded like below in some initializers file (may be in your tools.rb) :

yml文件可以像下面一样在一些初始化文件中加载(可能在你的tools.rb中):

     MY_TOOLS = YAML.load_file("#{RAILS_ROOT}/config/tools.yml")

Then you can use MY_TOOLS anywhere in app.

然后你可以在应用程序的任何地方使用MY_TOOLS。

#1


6  

You can keep all the extra stuff in either /app/modules or /lib. And i prefer lib. After putting in the lib folder, require it in any initializer (or create one)

您可以将所有额外的东西保存在/ app / modules或/ lib中。而且我更喜欢lib。放入lib文件夹后,在任何初始化程序中需要它(或创建一个)

require "./lib/tools" in /config/initializers/tools.rb

在/config/initializers/tools.rb中需要“./lib/tools”

And tadaa !! You can use that corresponding class/module anywhere in the rails application !!

和tadaa !!您可以在rails应用程序的任何位置使用相应的类/模块!

And all the YAML files should be put in /config/.

并且所有YAML文件都应该放在/ config /中。

*** fix syntax in '/lib/tools'

***修复'/ lib / tools'中的语法

#2


5  

You can include your own .rb files on lib folder. You can include, modules, class...etc under your own rb files.

您可以在lib文件夹中包含自己的.rb文件。您可以在自己的rb文件下包含,模块,类等。

If you want autoload or autorequire your custom library code, you must open your config/application.rb and add the next line for example:

如果要自动加载或自动查询自定义库代码,则必须打开config / application.rb并添加下一行,例如:

config.autoload_paths += %W(#{config.root}/extras #{config.root}/lib)

You can take a look to:

你可以看看:

http://reefpoints.dockyard.com/ruby/2012/02/14/love-your-lib-directory.html

http://reefpoints.dockyard.com/ruby/2012/02/14/love-your-lib-directory.html

You yaml files should be inside /config/ folder.

你yaml文件应该在/ config /文件夹中。

Regards!

问候!

#3


2  

If you have any module or class defined in script and use that module or class in app you just put it in lib . It will be accessed anywhere in app. Load the file if you need to initialize something which needs your application. Unless needed don't load things. If you think it must be loaded before app starts. Then you can put into config/initializers

如果您在脚本中定义了任何模块或类,并在应用程序中使用该模块或类,则只需将其放在lib中即可。它将在应用程序的任何位置访问。如果需要初始化需要应用程序的内容,请加载文件。除非需要不加载东西。如果您认为必须在应用程序启动之前加载它。然后你可以放入config / initializers

yml files can be loaded like below in some initializers file (may be in your tools.rb) :

yml文件可以像下面一样在一些初始化文件中加载(可能在你的tools.rb中):

     MY_TOOLS = YAML.load_file("#{RAILS_ROOT}/config/tools.yml")

Then you can use MY_TOOLS anywhere in app.

然后你可以在应用程序的任何地方使用MY_TOOLS。