是否有一个位置可以存储自定义 Ruby 库,以便 Ruby 可以找到它们?
我编写了一个模块,其中包含一些通用的、可重用的代码,我希望能够在其他项目中使用它们。是否有一个地方可以将此文件放在计算机上,以便 Ruby 可以找到它,无论我将包含该文件的文件保存在何处?我使用的是 Mac。
I have written a module that has some generic, reusable code that I would like to be able to use in other projects. Is there a place I could put this file on computer so that Ruby can find it regardless of where I saved the file that is including it? I am using a Mac.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有标准的地方可以放置这样的代码。您可以将所有代码放入 gem 中并安装 gem,或者创建一个目录来放入此代码。创建目录后,更改 LOAD_PATH 全局变量以包含此目录。您可以在使用这些的每个脚本中或使用 RUBYOPT 环境变量来执行此操作。例如,您可以将 ~/my_ruby_stuff 放在路径中并将文件放在那里。如果您这样做,请注意,请确保您添加的路径位于 gem 路径的末尾,并尽量避免与现有 Ruby 库或 gem 发生任何名称冲突。
There's no standard place to put code like this. You could put all your code in a gem and install the gem, or create a directory to put this code in. Once you create the directory, alter the LOAD_PATH global variable to include this directory. You can do this either in each script that uses these, or with the RUBYOPT environment variable. For example, you could put ~/my_ruby_stuff in your path and put your files there. One warning if you do that, make sure the path you add is at the end of the gem path and try to avoid any name conflicts with existing Ruby libraries or gems.
考虑将您的代码变成“宝石”。优点是:独立的项目、更好定义的接口、独立的源代码控制、可以与公司的其他开发人员共享等
Consider making a "gem" out of your code. The advantages are: separate project, better defined interface, separate source control, can share with other developers in your company, etc