gamingfan1993
November 21st, 2007, 03:39 PM
ARGV Tutorial
By: gamingfan1993 (although I'm not sure if this is the right place to post this)
After googling 'ruby ARGV', 'ruby arguments', and a number of other phrases, I have came upon the conclusion that tutorials for Ruby's ARGV[] array are too few and far between. There was a good one that explained ARGV[], but it focused mainly on the 'OptParse' library instead of the ARGV[] array. ARGV[] is an array that accepts command-line arguments based on the order that they come in on the command line.
Created command-line argumentative programs in Ruby is a much, much, easier task than doing so in C++, my other advanced language. Ruby allows you to use ARGV[] without declaring it earlier in your program, unlike C++, which you have to ask for it in 'int main(char argc, char* argv[])'. You can find out what arguments are by opening up DOS (or whatever your OS' command line is), and typing in a command, with some parameters. In MS-DOS you could type in 'dir C:\', in which 'dir' is the program, and 'C:\' is the argument. In order to use this in your program, you use the different 'slots' in the arrays. If we were to make a program called 'Hello', and give the user the choice to print whatever he/she wants to say 'Hello' to, we could use ARGV[]. I will make the program and we shall walk through it step by step.
# Hello Argument App
var1 = ARGV[0]
puts "Hello, #{var1}!"
Ok, copy-paste this code into Scite, or whatever editor you use for Ruby, and save it as 'hello.rb'. This will be the basis for your program. Preferably, you should save it on your root drive, this will make it much easier to access in MS-DOS. So whilst in your root drive, where 'hello.rb' should be, type this in your command-line:
hello World
In this command line program, hello is the program we created, and you just passed an argument, World, to it. Your output should be:
Hello, World!
Feel free to experiment as much as you want. Also; if you have any questions or comments, send them to me at pickensgamer@aol.com.
By: gamingfan1993 (although I'm not sure if this is the right place to post this)
After googling 'ruby ARGV', 'ruby arguments', and a number of other phrases, I have came upon the conclusion that tutorials for Ruby's ARGV[] array are too few and far between. There was a good one that explained ARGV[], but it focused mainly on the 'OptParse' library instead of the ARGV[] array. ARGV[] is an array that accepts command-line arguments based on the order that they come in on the command line.
Created command-line argumentative programs in Ruby is a much, much, easier task than doing so in C++, my other advanced language. Ruby allows you to use ARGV[] without declaring it earlier in your program, unlike C++, which you have to ask for it in 'int main(char argc, char* argv[])'. You can find out what arguments are by opening up DOS (or whatever your OS' command line is), and typing in a command, with some parameters. In MS-DOS you could type in 'dir C:\', in which 'dir' is the program, and 'C:\' is the argument. In order to use this in your program, you use the different 'slots' in the arrays. If we were to make a program called 'Hello', and give the user the choice to print whatever he/she wants to say 'Hello' to, we could use ARGV[]. I will make the program and we shall walk through it step by step.
# Hello Argument App
var1 = ARGV[0]
puts "Hello, #{var1}!"
Ok, copy-paste this code into Scite, or whatever editor you use for Ruby, and save it as 'hello.rb'. This will be the basis for your program. Preferably, you should save it on your root drive, this will make it much easier to access in MS-DOS. So whilst in your root drive, where 'hello.rb' should be, type this in your command-line:
hello World
In this command line program, hello is the program we created, and you just passed an argument, World, to it. Your output should be:
Hello, World!
Feel free to experiment as much as you want. Also; if you have any questions or comments, send them to me at pickensgamer@aol.com.