Command Line arguments in Ruby :(#isoeh) PROGRAMMING - TopicsExpress



          

Command Line arguments in Ruby :(#isoeh) PROGRAMMING TIPS: Lets us read today how to provide Command-line Arguments in Ruby. Command-line arguments are passed to the Ruby program by the shell, the program the expects commands (such as bash)on the terminal. Separated by spaces, each word or string are passed as a separate argument to the Ruby program. Lets take an example to launch the test.rb Ruby script from a command-line with the arguments test1 and test2. $ ./test.rb test1 test2 The tricky situation here is when you need to pass an argument with a space in it to a Ruby program. While it can seem complex, the trick is any arguments in double quotes will not be separated. The double quotes are removed by the shell before passing it to the Ruby program. Technique:- $ ./test.rb test1 test2 Furthermore 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. Sample syntax can be demonstrated as:- This program iterates over the ARGV array and prints out its contents. #!/usr/bin/env ruby ARGV.each do|a| puts Argument: #{a} end
Posted on: Sun, 16 Nov 2014 03:30:00 +0000

Trending Topics



Recently Viewed Topics




© 2015