gpt4 book ai didi

ruby - Ruby 中的 ARGV 有什么意义?

转载 作者:数据小太阳 更新时间:2023-10-29 07:40:14 26 4
gpt4 key购买 nike

Ruby 中的 ARGV 有什么意义?

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}"

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

ruby ex1.rb

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

ruby ex1.rb blah blah blah

这对运行程序的所有人有何好处?他们无论如何也做不到,因为我认为它是一个可执行文件:

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

谁能给我解释一下吗?

最佳答案

What's the point of ARGV in Ruby?

ARGV “包含传递给脚本的参数,每个元素一个。”

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.

这就是确切的要点,以便能够在运行文件时指定参数,例如:

ruby ex1.rb -a -b 3

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

任何其他命令行程序都能从中获得同样的好处:用户可以预先指定他们想要的内容,而且编写脚本比交互式 CLI 更容易。

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

是的,他们可以。您刚刚举例说明了用户将如何以这种方式运行您的程序。它是否可执行不会改变任何东西。

关于ruby - Ruby 中的 ARGV 有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13329132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com