使用Ruby在命令行中包含文件

时间:2021-11-23 00:14:24

When running ruby scripts as such

当运行ruby脚本时

ruby some-script.rb

How would I include a file (e.g. configuration file) in it dynamically?

如何动态地在其中包含文件(例如配置文件)?

5 个解决方案

#1


As you have found, the -r option is your friend. It also works with IRB:

如您所见,-r选项是您的朋友。它也适用于IRB:

irb -ropen-uri

Will do the same as require 'open-uri'

会像'open-uri'那样做

FWIW, the most common thing I need to include via the command line is rubygems. And since newer versions of ruby come with gems built in I don't want to edit the file, but include it for testing. Luckily the folks who created gems added a little alias sugar.

FWIW,我需要通过命令行包含的最常见的东西是rubygems。而且由于新版本的ruby内置了宝石,我不想编辑文件,而是将其包含在测试中。幸运的是,创造宝石的人们添加了一点别名的糖。

You can do the following:

您可以执行以下操作:

ruby -rubygems myscript.rb

Instead of the ugly:

而不是丑陋的:

ruby -rrubygems myscript.rb

OK, so it is one character, but thought it was extra polish to make me happier.

好的,所以这是一个角色,但认为让我更快乐是额外的润色。

#2


Actually, I found it. It's the -r command line entry.

实际上,我发现了它。这是-r命令行条目。

#3


You can use:

您可以使用:

require 'some_ruby_file'

in some-script.rb. It will load some_ruby_file.rb.

在some-script.rb中。它将加载some_ruby_file.rb。

#4


Before you call require "somefile.rb" you must navigate to the folder that the file is located or you must provide the full path. In example: require "~/Documents/Somefolder/somefile.rb"

在调用require“somefile.rb”之前,必须导航到文件所在的文件夹,或者必须提供完整路径。例如:require“〜/ Documents / Somefolder / somefile.rb”

#5


 -r <library_name>

This causes Ruby to load the library using require.

这导致Ruby使用require加载库。

It is useful when used in conjunction with -n or -p.

与-n或-p一起使用时非常有用。

#1


As you have found, the -r option is your friend. It also works with IRB:

如您所见,-r选项是您的朋友。它也适用于IRB:

irb -ropen-uri

Will do the same as require 'open-uri'

会像'open-uri'那样做

FWIW, the most common thing I need to include via the command line is rubygems. And since newer versions of ruby come with gems built in I don't want to edit the file, but include it for testing. Luckily the folks who created gems added a little alias sugar.

FWIW,我需要通过命令行包含的最常见的东西是rubygems。而且由于新版本的ruby内置了宝石,我不想编辑文件,而是将其包含在测试中。幸运的是,创造宝石的人们添加了一点别名的糖。

You can do the following:

您可以执行以下操作:

ruby -rubygems myscript.rb

Instead of the ugly:

而不是丑陋的:

ruby -rrubygems myscript.rb

OK, so it is one character, but thought it was extra polish to make me happier.

好的,所以这是一个角色,但认为让我更快乐是额外的润色。

#2


Actually, I found it. It's the -r command line entry.

实际上,我发现了它。这是-r命令行条目。

#3


You can use:

您可以使用:

require 'some_ruby_file'

in some-script.rb. It will load some_ruby_file.rb.

在some-script.rb中。它将加载some_ruby_file.rb。

#4


Before you call require "somefile.rb" you must navigate to the folder that the file is located or you must provide the full path. In example: require "~/Documents/Somefolder/somefile.rb"

在调用require“somefile.rb”之前,必须导航到文件所在的文件夹,或者必须提供完整路径。例如:require“〜/ Documents / Somefolder / somefile.rb”

#5


 -r <library_name>

This causes Ruby to load the library using require.

这导致Ruby使用require加载库。

It is useful when used in conjunction with -n or -p.

与-n或-p一起使用时非常有用。