Ruby中的ARGV有什么意义? [英] What's the point of ARGV in Ruby?

查看:417
本文介绍了Ruby中的ARGV有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中ARGV的意义是什么?

What's the point of ARGV in Ruby?

first, second, third = ARGV 
puts "The script is called: #{$0}"
puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"

什么时候运行我需要做的文件有什么意义?

What's the point of this when to run the file I need to do:

ruby ex1.rb

并输入我需要输入的第一个,第二个和第三个变量

and to put in the first, second and third variables I need to type in

ruby ex1.rb blah blah blah

这对运行该程序的所有人有何好处?因为我认为它是可执行文件,所以他们无论如何都不能这样做:

How does this benefit at all the person running the program? They can't do it anyway since I'd assume it be an executable:

user = ARGV.first
prompt = '> '

puts "Hi #{user}, I'm the #{$0} script."
puts "I'd like to ask you a few questions."
puts "Do you like me #{user}?"
print prompt
likes = STDIN.gets.chomp()

puts "Where do you live #{user}?"
print prompt
lives = STDIN.gets.chomp()

puts "What kind of computer do you have?"
print prompt
computer = STDIN.gets.chomp()

puts <<MESSAGE
Alright, so you said #{likes} about liking me.
You live in #{lives}.  Not sure where that is.
And you have a #{computer} computer.  Nice.
MESSAGE

有人可以向我解释一下吗?

Can someone please explain this to me?

推荐答案

在Ruby中ARGV有什么意义?

ARGV 包含参数传递给脚本,每个元素一个."

ARGV "contains the arguments passed to your script, one per element."

什么时候运行您需要做的文件:ruby ex1.rb并放入您需要键入ruby ex1.rb blah blah blah的第一个,第二个和第三个变量.

What's the point of this when to run the file you need to do: ruby ex1.rb and to put in the first, second and third variables you need to type in ruby ex1.rb blah blah blah.

确切的一点是,可以在运行文件时指定参数,例如:

That is the exact point, to be able to specify arguments when running the file like:

ruby ex1.rb -a -b 3

这对所有运行该程序的人有什么好处?

How does this benefit at all the person running the program?

任何其他命令行程序都能从中获得相同的好处:用户可以预先指定所需内容,并且比交互式CLI编写脚本更容易.

Same benefit any other command-line program gets from being able to do so: the user can specify upfront what they want, and it's easier to script than an interactive CLI.

因为我认为它是可执行文件,所以他们无论如何也不能这样做.

They can't do it anyway since I'd assume it be an executable.

是的,他们可以.您仅给出了一个示例,确切说明了用户将如何以这种方式运行您的程序.不管它是否可执行,都不会改变.

Yes, they can. You just gave an example of exactly how the user would run your program that way. Whether it's executable or not doesn't change anything.

这篇关于Ruby中的ARGV有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆