What's the point of ARGV in Ruby?

后端 未结 5 916
旧巷少年郎
旧巷少年郎 2020-12-06 03:31

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 se         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-06 04:12

    What's the point of ARGV in Ruby?

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

    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?

    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.

提交回复
热议问题