ruby 安装命令行_Ruby中的命令行参数

ruby 安装命令行

Many Ruby scripts have no text or graphical interfaces. They simply run, do their job and then exit. To communicate with these scripts in order to change their behavior, command-line arguments must be used.

许多Ruby脚本没有文本或图形界面 。 他们只是跑,做自己的工作,然后退出。 要与这些脚本进行通信以更改其行为,必须使用命令行参数。

The command line is the standard mode of operation for UNIX commands, and since Ruby is used widely on UNIX and UNIX-like systems (such as Linux and macOS), it's pretty standard to encounter this type of program.

命令行是UNIX命令的标准操作模式,并且由于Ruby在UNIX和类似UNIX的系统(例如Linux和macOS)上得到了广泛使用,因此遇到此类程序是相当标准的。

如何提供命令行参数 ( How to Provide Command-Line Arguments )

Ruby script arguments are passed to the Ruby program by the shell, the program that accepts commands (such as bash) on the terminal.

外壳程序将Ruby脚本参数传递给Ruby程序,该程序在终端上接受命令(例如bash)。

On the command-line, any text following the name of the script is considered a command-line argument. Separated by spaces, each word or string will be passed as a separate argument to the Ruby program. 

在命令行上,脚本名称后的任何文本均被视为命令行参数。 用空格分隔,每个单词或字符串将作为单独的参数传递给Ruby程序。

The following example shows the proper syntax to use to launch the test.rb Ruby script from a command-line with the arguments test1 and test2.

以下示例显示了用于从命令行通过参数test1test2启动test.rb Ruby脚本的正确语法。


$ ./test.rb test1 test2

You may encounter a situation in which you need to pass an argument to a Ruby program but there's a space in the command. It seems impossible at first since the shell separates arguments on spaces, but there is a provision for this.

您可能会遇到需要将参数传递给Ruby程序,但命令中有空格的情况。 乍一看似乎是不可能的,因为外壳程序将空格参数分开,但是对此有规定。

Any arguments in double quotes will not be separated. The double quotes are removed by the shell before passing it to the Ruby program.

双引号中的任何参数都不会分开。 在将双引号传递给Ruby程序之前,shell会删除双引号。

The following example passes a single argument to the test.rb Ruby script, test1 test2:

下面的示例将单个参数传递给test.rb Ruby脚本test1 test2


$ ./test.rb "test1 test2"

如何使用命令行参数 ( How to Use Command-Line Arguments )

In your Ruby programs, you can access any command-line arguments passed by the shell with the ARGV special variable. ARGV is an Array variable which holds, as strings, each argument passed by the shell.

在Ruby程序中,您可以使用ARGV特殊变量访问外壳传递的任何命令行参数。 ARGV是一个Array变量,它以字符串形式存储shell传递的每个参数。

This program iterates over the ARGV array and prints out its contents:

该程序遍历ARGV 数组并打印出其内容:


#!/usr/bin/env ruby
ARGV.each do|a|
  puts "Argument: #{a}"
end

The following is an excerpt of a bash session launching this script (saved as the file test.rb) with a variety of arguments:

以下是bash会话的摘录,该会话使用各种参数启动此脚本(保存为文件test.rb ):


$ ./test.rb test1 test2 "three four"
Argument: test1
Argument: test2
Argument: three four

翻译自: https://www.thoughtco.com/command-line-arguments-2908191

ruby 安装命令行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值