3 Certain command-line options affect the execution environment
4 of the invoked Ruby program.
8 The examples here use command-line option `-e`,
9 which passes the Ruby code to be executed on the command line itself:
12 $ ruby -e 'puts "Hello, World."'
17 The argument to option `-C` specifies a working directory
18 for the invoked Ruby program;
19 does not change the working directory for the current process:
24 $ ruby -C lib -e 'puts File.basename(Dir.pwd)'
30 Whitespace between the option and its argument may be omitted.
34 The argument to option `-I` specifies a directory
35 to be added to the array in global variable `$LOAD_PATH`;
36 the option may be given more than once:
40 $ ruby -e 'p $LOAD_PATH.size'
42 $ ruby -I my_lib -I some_lib -e 'p $LOAD_PATH.size'
44 $ ruby -I my_lib -I some_lib -e 'p $LOAD_PATH.take(2)'
45 ["/tmp/my_lib", "/tmp/some_lib"]
49 Whitespace between the option and its argument may be omitted.
53 The argument to option `-r` specifies a library to be required
54 before executing the Ruby program;
55 the option may be given more than once:
58 $ ruby -e 'p defined?(JSON); p defined?(CSV)'
61 $ ruby -r CSV -r JSON -e 'p defined?(JSON); p defined?(CSV)'
66 Whitespace between the option and its argument may be omitted.
70 Option `-0` defines the input record separator `$/`
71 for the invoked Ruby program.
73 The optional argument to the option must be octal digits,
74 each in the range `0..7`;
75 these digits are prefixed with digit `0` to form an octal value:
77 - If no argument is given, the input record separator is `0x00`.
78 - If the argument is `0`, the input record separator is `''`;
79 see {Special Line Separator Values}[rdoc-ref:IO@Special+Line+Separator+Values].
80 - If the argument is in range `(1..0377)`,
81 it becomes the character value of the input record separator `$/`.
82 - Otherwise, the input record separator is `nil`.
95 $ ruby -0377 -e 'p $/'
97 $ ruby -0400 -e 'p $/'
101 The option may not be separated from its argument by whitespace.
105 Some code in (or called by) the Ruby program may include statements or blocks
106 conditioned by the global variable `$DEBUG` (e.g., `if $DEBUG`);
107 these commonly write to `$stdout` or `$stderr`.
109 The default value for `$DEBUG` is `false`;
110 option `-d` (or `--debug`) sets it to `true`:
115 $ ruby -d -e 'p $DEBUG'
121 Option `-w` (lowercase letter) is equivalent to option `-W1` (uppercase letter).
125 Any Ruby code can create a <i>warning message</i> by calling method Kernel#warn;
126 methods in the Ruby core and standard libraries can also create warning messages.
127 Such a message may be printed on `$stderr`
128 (or not, depending on certain settings).
130 Option `-W` helps determine whether a particular warning message
132 by setting the initial value of global variable `$-W`:
134 - `-W0`: Sets `$-W` to `0` (silent; no warnings).
135 - `-W1`: Sets `$-W` to `1` (moderate verbosity).
136 - `-W2`: Sets `$-W` to `2` (high verbosity).
137 - `-W`: Same as `-W2` (high verbosity).
138 - Option not given: Same as `-W1` (moderate verbosity).
140 The value of `$-W`, in turn, determines which warning messages (if any)
141 are to be printed to `$stdout` (see Kernel#warn):
144 $ ruby -W1 -e 'p $foo'
146 $ ruby -W2 -e 'p $foo'
147 -e:1: warning: global variable '$foo' not initialized
151 Ruby code may also define warnings for certain categories;
152 these are the default settings for the defined categories:
155 Warning[:experimental] # => true
156 Warning[:deprecated] # => false
157 Warning[:performance] # => false
160 They may also be set:
162 Warning[:experimental] = false
163 Warning[:deprecated] = true
164 Warning[:performance] = true
167 You can suppress a category by prefixing `no-` to the category name:
170 $ ruby -W:no-experimental -e 'p IO::Buffer.new'