Ruby - 如何使用-r switch with ruby​​命令行工具

时间:2022-06-15 14:56:15

I was trying to figure out how to work the command line switch -r.

我试图找出如何使用命令行开关-r。

My understanding is that the code is typed out as follows:

我的理解是代码输入如下:

ruby -r*nameOfRequired*

I am finding that this is not the case. When I type out the above and press enter, the terminal expects an "end of input syntax" and does not continue.

我发现事实并非如此。当我键入以上内容并按Enter键时,终端需要“输入语法结束”并且不会继续。

What am I missing? Does there need to be a space in between the switch and the name of the required file?

我错过了什么?交换机和所需文件的名称之间是否需要有空格?

Please and thank you!

谢谢,麻烦您了!


EDIT: I am currently reading "The Well Grounded Rubyist" by David A. Black, and I came up with this question while reading the section on command line switches.

编辑:我目前正在阅读David A. Black的“The Well Grounded Rubyist”,我在阅读命令行开关部分时提出了这个问题。

Having said that, I created a "test.rb" file, containing:

话虽如此,我创建了一个“test.rb”文件,其中包含:

puts Date.today

Then, in the terminal, I typed out:

然后,在终端,我键入:

ruby -r date

I thought this would 'require' the date module, and then enable me to run the "test.rb" file, using ruby test.rb (given that I am in the correct directory).

我认为这将“需要”日期模块,然后使我能够运行“test.rb”文件,使用ruby test.rb(假设我在正确的目录中)。

Instead, the terminal cursor moves to a newline, expecting more input. Let me know if I need to clarify anything else. Thanks!

相反,终端光标移动到换行符,期望更多输入。如果我需要澄清其他任何内容,请告诉我。谢谢!

3 个解决方案

#1


1  

If you just type ruby -rmodule, then Ruby will load the module and wait for you to type the main program that requires that module.

如果只输入ruby -rmodule,则Ruby将加载模块并等待您键入需要该模块的主程序。

If you just want to run the module and do nothing else, you can do do rubyfull-path-to-module without the -r, or ruby -rmodule -e exit, or ruby -rmodule </dev/null, or similar.

如果您只是想运行该模块而不执行任何其他操作,则可以执行rubyfull-path-to-module而不使用-r或ruby -rmodule -e exit,或者ruby -rmodule

#2


0  

According to ruby -h:

根据ruby -h:

-rlibrary require the library, before executing your script

-rlibrary在执行脚本之前需要库

Without giving your script file path, it read the script from stdin.

在不提供脚本文件路径的情况下,它从stdin读取脚本。

Try following (You can omit script file path when you give -e command):

尝试以下(您可以在给出-e命令时省略脚本文件路径):

ruby -r**nameOfRequired** -e ""

#3


0  

In general, the ruby command does not record any state from one run to the next, so you need to tell it every thing that it needs to know whenever you run it.

通常,ruby命令不会记录从一次运行到下一次运行的任何状态,因此您需要告诉它每次运行时都需要知道的事情。

Whenever you run it, you need to tell it the main program to run or else it will expect you to type that program on the standard input. The -r does not specify the main program.

无论何时运行它,您都需要告诉它要运行的主程序,否则它会指望您在标准输入上键入该程序。 -r未指定主程序。

Try this:

ruby -rdate test.rb

#1


1  

If you just type ruby -rmodule, then Ruby will load the module and wait for you to type the main program that requires that module.

如果只输入ruby -rmodule,则Ruby将加载模块并等待您键入需要该模块的主程序。

If you just want to run the module and do nothing else, you can do do rubyfull-path-to-module without the -r, or ruby -rmodule -e exit, or ruby -rmodule </dev/null, or similar.

如果您只是想运行该模块而不执行任何其他操作,则可以执行rubyfull-path-to-module而不使用-r或ruby -rmodule -e exit,或者ruby -rmodule

#2


0  

According to ruby -h:

根据ruby -h:

-rlibrary require the library, before executing your script

-rlibrary在执行脚本之前需要库

Without giving your script file path, it read the script from stdin.

在不提供脚本文件路径的情况下,它从stdin读取脚本。

Try following (You can omit script file path when you give -e command):

尝试以下(您可以在给出-e命令时省略脚本文件路径):

ruby -r**nameOfRequired** -e ""

#3


0  

In general, the ruby command does not record any state from one run to the next, so you need to tell it every thing that it needs to know whenever you run it.

通常,ruby命令不会记录从一次运行到下一次运行的任何状态,因此您需要告诉它每次运行时都需要知道的事情。

Whenever you run it, you need to tell it the main program to run or else it will expect you to type that program on the standard input. The -r does not specify the main program.

无论何时运行它,您都需要告诉它要运行的主程序,否则它会指望您在标准输入上键入该程序。 -r未指定主程序。

Try this:

ruby -rdate test.rb