w3resource

Ruby Command Line Options

Command Line Options

A Ruby command line consists of three parts:

  • Options to the Ruby interpreter.
  • Optionally the name of a program to run.
  • Optionally a set of arguments for that program.

Syntax:

ruby [
            options
            ] [--] [
            programfile
            ] [
            arguments
            ]

The interpreter can be invoked with any of the following options to control the environment and behavior of the interpreter.

Option Description
-a Used with -n or -p to split each line. Check -n and -p options.
Auto split mode when used with -n or -p; equivalent to executing {$F at the top of each loop iteration.
-c Does not execute the program, only checks syntax.
-Cdirectory Changes directory before executing (equivalent to -X).
Changes working directory to directory before executing.
--copyright Prints the copyright notice and exits.
-d, --debug Enables debug mode (equivalent to -debug).
Sets $DEBUG to true. This can be used by your programs to enable additional tracing.
-e 'command' Executes command as one line of Ruby source. Several -e's are allowed, and the commands are treated as multiple lines in the same program. If program file is omitted when -e is present, execution stops after the -e commands have been run.
-F pattern

Specifies pat as the default separator pattern ($;) used by split.

Specifies the input field separator ($;) used as the default for split() (affects -a).
-h Displays a short help screen.
-i [extension} Overwrites the file contents with program output. The original file is saved with the extension ext. If ext isn't specified, the original file is deleted. Edits ARGV files in place. For each file named in ARGV, anything you write to standard output will be saved back as the contents of that file. A backup copy of the file will be made if extension is supplied.
-I directories

Adds dir as the directory for loading libraries.

Specifies directories to be prepended to $LOAD_PATH ($:). Multiple -I options may be present, and multiple directories may appear following each -I. Directories are separated by a ``:'' on Unix-like systems and by a ``;'' on DOS/Windows systems.
-K kcode Specifies the multibyte character set code (e or E for EUC (extended Unix code); s or S for SJIS (Shift-JIS); u or U for UTF-8; and a, A, n, or N for ASCII). Specifies the code set to be used. This option is useful mainly when Ruby is used for Japanese-language processing. kcode may be one of: e, E for EUC; s, S for SJIS; u, U for UTF-8; or a, A, n, N for ASCII.
-l Enables automatic line-end processing. Chops a newline from input lines and appends a newline to output lines. Enables automatic line-ending processing; sets $\ to the value of $/ and chops every input line automatically.
-n Places code within an input loop (as in while gets; ... end). Assumes ``while gets; ...; end'' loop around your program. For example, a simple grep command might be implemented as:
-p Places code within an input loop. Writes $_ for each iteration. Places your program code within the loop ``while gets; ...; print; end.
-r library Uses require to load lib as a library before executing. requires the named library before executing.
-S Looks for the program file using RUBYPATH or PATH environment variable.
-s Interprets any arguments between the program name and filename arguments fitting the pattern -sss as a switch and defines the corresponding variable. Any command line switches found after the program filename, but before any filename arguments or before a --, are removed from ARGV and set to a global variable named for the switch. In the following example, the effect of this would be to set the variable $opt to ``electric''.
-T[level} Sets the level for tainting checks (1 if level not specified). Sets the safe level, which among other things enables tainting checks (see page 253). Sets $SAFE.
-v, --verbose Enables verbose mode and print the version number. In verbose mode, compilation warnings are printed.
--version Displays the Ruby version number and exits.
-w Enables verbose mode. If program file not specified, reads the from STDIN.
Enables verbose mode. Unlike -v, reads program from standard input if no program files are present on the command line. We recommend running your Ruby programs with -w.
-x [directory} Strips text before #!ruby line. Changes directory to dir before executing if dir is specified.
Strips off text before #!ruby line and changes working directory to directory if given.
-X directory Changes directory before executing (equivalent to -C).
Changes working directory to directory before executing. Same as -C directory.
-y, --yydebug Enables parser to debug mode.
Enables yacc debugging in the parser (waaay too much information).

Some examples of command Line options :

Execute a file abc.rb with -c option

rg@server:~$ ruby -c abc.rb
Syntax OK
rg@server:~$

Execute a file abc.rb with -C option

rg@server:~$ ls
abc.rb  con.py  co.py  test
rg@server:~$ cd test
rg@server:~/test$ ls
rg@server:~/test$ ruby -C /home/rg  abc.rb
Ruby Tutorial
rg@server:~/test$

Execute a file abc.rb with -v option

H:\>ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [i386-mingw32]
H:\>ruby -verbose
ruby 2.2.2p95 (2015-04-13 revision 50295) [i386-mingw32]
-e:1:in `<main>': undefined local 
variable or method `rbose' for main:Object
(NameError)
Execute a file abc.rb with --copyright option
rg@server:~$ ruby --copyright 
ruby - Copyright (C) 1993-2015 Yukihiro Matsumoto

Previous: Ruby Installation on Windows
Next: Interactive Ruby



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/ruby/ruby-command-line-options.php