不同Ruby项目之间代码重用的最佳实践是什么?

时间:2023-01-19 20:07:25

guys!

I'm a software developer with Java background and I'm starting some projects using a Ruby web framework (Padrino/Sinatra).

我是一名具有Java背景的软件开发人员,我正在使用Ruby Web框架(Padrino / Sinatra)开始一些项目。

In my java projects, I usually had some "common" projects whose classes where used in several projects. For instance, I had a central authentication service, and a shared database that stored the user profiles. All my projects that used this service shared some models mapped to the user profile database.

在我的java项目中,我通常有一些“常见”项目,其中的类在几个项目中使用。例如,我有一个*身份验证服务,以及一个存储用户配置文件的共享数据库。我使用此服务的所有项目共享一些映射到用户配置文件数据库的模型。

So, despite the framework, orm lib etc., what's the best way of sharing code across several Ruby projects?

所以,尽管有框架,orm lib等,但是跨多个Ruby项目共享代码的最佳方式是什么?

2 个解决方案

#1


4  

Besides this, ruby's gems is one of the best way of reusing common parts of code. Gems have names, version numbers, and descriptions and therefore you can easily maintain up-to-date versions of these libraries, install and uninstall, manage your computer’s local installations of gems using the gem command, available from the command line. Gems became standard with Ruby 1.9, but before you have to use require 'rubygems' line in the scripts. There are several tools that help create them, for example, bundler. Bundler not only tool for gem creation, but an awesome application's dependencies manager.

除此之外,ruby的宝石是重用代码常用部分的最佳方式之一。 Gems具有名称,版本号和描述,因此您可以使用命令行中提供的gem命令轻松维护这些库的最新版本,安装和卸载,管理计算机的本地宝石安装。 Gems成为Ruby 1.9的标准,但在脚本中必须使用require'rubygems'之前。有几种工具可以帮助创建它们,例如,bundler。 Bundler不仅是用于创建gem的工具,还是一个非常棒的应用程序的依赖项管理器。

#2


0  

Put your code into a something.rb file and require it at the top of your other script(s).

将您的代码放入something.rb文件中,并将其放在其他脚本的顶部。

You can also use load instead of require, but require has the nice property that it will not include a file more than once. Also, using load requires the .rb extension, whereas require does not, i.e.,

您也可以使用load而不是require,但require具有不会包含多个文件的nice属性。此外,使用load需要.rb扩展名,而require则不需要.rb扩展名,即

#some_script.rb
puts('hello world')

#another script
require 'some_script'
>> hello world

load 'some_script'
LoadError: no such file to load -- some_script
    from (irb):2:in 'load'
    from (irb):2

You will almost always use require, but load is an option as well if you want to use it for... whatever reason.

您几乎总是使用require,但如果您想将其用于...无论什么原因,也可以选择加载。

#1


4  

Besides this, ruby's gems is one of the best way of reusing common parts of code. Gems have names, version numbers, and descriptions and therefore you can easily maintain up-to-date versions of these libraries, install and uninstall, manage your computer’s local installations of gems using the gem command, available from the command line. Gems became standard with Ruby 1.9, but before you have to use require 'rubygems' line in the scripts. There are several tools that help create them, for example, bundler. Bundler not only tool for gem creation, but an awesome application's dependencies manager.

除此之外,ruby的宝石是重用代码常用部分的最佳方式之一。 Gems具有名称,版本号和描述,因此您可以使用命令行中提供的gem命令轻松维护这些库的最新版本,安装和卸载,管理计算机的本地宝石安装。 Gems成为Ruby 1.9的标准,但在脚本中必须使用require'rubygems'之前。有几种工具可以帮助创建它们,例如,bundler。 Bundler不仅是用于创建gem的工具,还是一个非常棒的应用程序的依赖项管理器。

#2


0  

Put your code into a something.rb file and require it at the top of your other script(s).

将您的代码放入something.rb文件中,并将其放在其他脚本的顶部。

You can also use load instead of require, but require has the nice property that it will not include a file more than once. Also, using load requires the .rb extension, whereas require does not, i.e.,

您也可以使用load而不是require,但require具有不会包含多个文件的nice属性。此外,使用load需要.rb扩展名,而require则不需要.rb扩展名,即

#some_script.rb
puts('hello world')

#another script
require 'some_script'
>> hello world

load 'some_script'
LoadError: no such file to load -- some_script
    from (irb):2:in 'load'
    from (irb):2

You will almost always use require, but load is an option as well if you want to use it for... whatever reason.

您几乎总是使用require,但如果您想将其用于...无论什么原因,也可以选择加载。