|
Programmers prefer to run Ruby from the command line like many other scripting language interpreters. We have the facility of providing the name of a program file along with the command-line switches. These command-line switches instruct the interpreter to perform the functions as required by the programmer.
Ruby provides us with the command-line switches which are more than twenty in number.
Let’s discuss few of these commonly implemented switches with their syntaxes and examples:
1. –c: This is implemented to check the syntax of a program file without executing the program.It is mostly implemented with the -w switch as they both when taken together help the developer in checking the syntax and also provides the warning message is reuired to be given to the developer.
syntax:ruby -c filename.ext
example: ruby -c abc.rb
2. -w : This switch will provide warning messages during program execution to the developer.
syntax:ruby -w filename.ext
example:ruby -w abs.rb
3. -e : It executes the actual code provided in quotation marks on the command line instead of code in the file.
syntax:ruby -e 'instruction to execute"code segment"'
example:ruby -e 'puts "Demo Code"'
4. -v : It shows Ruby version information, and execute the program in verbose mode.
syntax: ruby -v
example:ruby -v
5. –version: It shows Ruby version information similar to -v commnadline but never executes any code even if it is written after the - version. It just prints the version number anf immediately exits from there.
syntax:ruby --version
example:ruby --version
6. Check syntax (-c): The -c switch instructs Ruby to check the code in the file for its accuracy without executing the code.
syntax:ruby -c filename.ext
example:ruby -c xyz.rb
7. –a: This command line is used with -n or -p to split each line.
syntax: ruby -a filename.ext
example:ruby -a schoollist.ext
8.-C dir: It changes the working directory to given directory before executing.
syntax:ruby -C directory name
example:ruby -C
9.-d: This enables debug mode. This command is very much similar to the -debug command line which also enables the debug mode.
syntax:ruby -d
10.-e prog : This command line is used to specify prog as the program from the command line.
11.-h: It displays an overview of command-line options.
syntax:ruby -h
12. -i [ext]: This overwrites the file contents with program output. In this the original file is saved with the extension ext. The original file gets deleted if ext isn't specified by the programmer.
syntax:ruby -i[ext]
13. -I dir: It adds dir as the directory for loading libraries.
syntax:ruby -I dirname
14. –l: It enables automatic line-end processing.
syntax: ruby -l filename.ext
example:ruby -l lastname.rb
let us say that this file lastname.rb contains four names james willy lyn mathew ,
without -l in command line will give the output as:
jameswillylynmathew
while putting -l in command line will give output as
james
willy
lyn
mathew
15. -p: This switch is used to put the code within an input loop. It automatically writes $_ for each iteration.
16. -X dir: It changes the directory before executing. It is very similar to -c switch.
syntax:ruby -X directoryname
17. –copyright: It is used to display the copyright notice.
syntax:ruby -copyright
18. —debug: It will enable the debug mode.
syntax:ruby -debug
19. —help: It will display an overview of command-line options.
syntax: ruby -help
20. —version: It is used to display version.
syntax: ruby -version
|