1RUBY(1) Ruby Programmers Reference Guide RUBY(1)
2
4 ruby — Interpreted object-oriented scripting language
5
7 ruby [--copyright] [--version] [-SUacdlnpswvy] [-0[octal]] [-C directory]
8 [-E external[:internal]] [-F pattern] [-I directory] [-K c]
9 [-T[level]] [-W[level]] [-e command] [-i[extension]] [-r library]
10 [-x[directory]] [-{enable|disable}-FEATURE] [--dump=target]
11 [--verbose] [--] [program_file] [argument ...]
12
14 Ruby is an interpreted scripting language for quick and easy object-ori‐
15 ented programming. It has many features to process text files and to do
16 system management tasks (like in Perl). It is simple, straight-forward,
17 and extensible.
18
19 If you want a language for easy object-oriented programming, or you don't
20 like the Perl ugliness, or you do like the concept of LISP, but don't
21 like too many parentheses, Ruby might be your language of choice.
22
24 Ruby's features are as follows:
25
26 Interpretive
27 Ruby is an interpreted language, so you don't have to recompile
28 programs written in Ruby to execute them.
29
30 Variables have no type (dynamic typing)
31 Variables in Ruby can contain data of any type. You don't have
32 to worry about variable typing. Consequently, it has a weaker
33 compile time check.
34
35 No declaration needed
36 You can use variables in your Ruby programs without any declara‐
37 tions. Variable names denote their scope - global, class,
38 instance, or local.
39
40 Simple syntax
41 Ruby has a simple syntax influenced slightly from Eiffel.
42
43 No user-level memory management
44 Ruby has automatic memory management. Objects no longer refer‐
45 enced from anywhere are automatically collected by the garbage
46 collector built into the interpreter.
47
48 Everything is an object
49 Ruby is a purely object-oriented language, and was so since its
50 creation. Even such basic data as integers are seen as objects.
51
52 Class, inheritance, and methods
53 Being an object-oriented language, Ruby naturally has basic fea‐
54 tures like classes, inheritance, and methods.
55
56 Singleton methods
57 Ruby has the ability to define methods for certain objects. For
58 example, you can define a press-button action for certain widget
59 by defining a singleton method for the button. Or, you can make
60 up your own prototype based object system using singleton meth‐
61 ods, if you want to.
62
63 Mix-in by modules
64 Ruby intentionally does not have the multiple inheritance as it
65 is a source of confusion. Instead, Ruby has the ability to share
66 implementations across the inheritance tree. This is often
67 called a ‘Mix-in’.
68
69 Iterators
70 Ruby has iterators for loop abstraction.
71
72 Closures
73 In Ruby, you can objectify the procedure.
74
75 Text processing and regular expressions
76 Ruby has a bunch of text processing features like in Perl.
77
78 M17N, character set independent
79 Ruby supports multilingualized programming. Easy to process texts
80 written in many different natural languages and encoded in many
81 different character encodings, without dependence on Unicode.
82
83 Bignums
84 With built-in bignums, you can for example calculate facto‐
85 rial(400).
86
87 Reflection and domain specific languages
88 Class is also an instance of the Class class. Definition of
89 classes and methods is an expression just as 1+1 is. So your pro‐
90 grams can even write and modify programs. Thus you can write
91 your application in your own programming language on top of Ruby.
92
93 Exception handling
94 As in Java(tm).
95
96 Direct access to the OS
97 Ruby can use most UNIX system calls, often used in system pro‐
98 gramming.
99
100 Dynamic loading
101 On most UNIX systems, you can load object files into the Ruby
102 interpreter on-the-fly.
103
104 Rich libraries
105 Libraries called "builtin libraries" and "standard libraries" are
106 bundled with Ruby. And you can obtain more libraries via the
107 package management system called `RubyGems'.
108
109 Moreover there are thousands of Ruby projects on GitHub
110 <https://github.com/languages/Ruby>.
111
113 Ruby interpreter accepts following command-line options (switches). They
114 are quite similar to those of perl(1).
115
116 --copyright Prints the copyright notice.
117
118 --version Prints the version of Ruby interpreter.
119
120 -0[octal] (The digit “zero”.) Specifies the input record separator
121 ($/) as an octal number. If no digit is given, the null
122 character is taken as the separator. Other switches may
123 follow the digits. -00 turns Ruby into paragraph mode.
124 -0777 makes Ruby read whole file at once as a single
125 string since there is no legal character with that value.
126
127 -C directory
128 -X directory Causes Ruby to switch to the directory.
129
130 -E external[:internal]
131 --encoding external[:internal]
132 Specifies the default value(s) for external encodings and
133 internal encoding. Values should be separated with colon
134 (:).
135
136 You can omit the one for internal encodings, then the
137 value (Encoding.default_internal) will be nil.
138
139 --external-encoding=encoding
140 --internal-encoding=encoding
141 Specify the default external or internal character encod‐
142 ing
143
144 -F pattern Specifies input field separator ($;).
145
146 -I directory Used to tell Ruby where to load the library scripts.
147 Directory path will be added to the load-path variable
148 ($:).
149
150 -K kcode Specifies KANJI (Japanese) encoding. The default value for
151 script encodings (__ENCODING__) and external encodings
152 (Encoding.default_external) will be the specified one.
153 kcode can be one of
154
155 e EUC-JP
156
157 s Windows-31J (CP932)
158
159 u UTF-8
160
161 n ASCII-8BIT (BINARY)
162
163 -S Makes Ruby use the PATH environment variable to search for
164 script, unless its name begins with a slash. This is used
165 to emulate #! on machines that don't support it, in the
166 following manner:
167
168 #! /usr/local/bin/ruby
169 # This line makes the next one a comment in Ruby \
170 exec /usr/local/bin/ruby -S $0 $*
171
172 -T[level=1] Turns on taint checks at the specified level (default 1).
173
174 -U Sets the default value for internal encodings
175 (Encoding.default_internal) to UTF-8.
176
177 -W[level=2] Turns on verbose mode at the specified level without
178 printing the version message at the beginning. The level
179 can be;
180
181 0 Verbose mode is "silence". It sets the
182 $VERBOSE to nil.
183
184 1 Verbose mode is "medium". It sets the
185 $VERBOSE to false.
186
187 2 (default) Verbose mode is "verbose". It sets the
188 $VERBOSE to true. -W2 is same as -w
189
190 -a Turns on auto-split mode when used with -n or -p. In
191 auto-split mode, Ruby executes
192 $F = $_.split
193 at beginning of each loop.
194
195 -c Causes Ruby to check the syntax of the script and exit
196 without executing. If there are no syntax errors, Ruby
197 will print “Syntax OK” to the standard output.
198
199 -d
200 --debug Turns on debug mode. $DEBUG will be set to true.
201
202 -e command Specifies script from command-line while telling Ruby not
203 to search the rest of the arguments for a script file
204 name.
205
206 -h
207 --help Prints a summary of the options.
208
209 -i extension Specifies in-place-edit mode. The extension, if speci‐
210 fied, is added to old file name to make a backup copy.
211 For example:
212
213 % echo matz > /tmp/junk
214 % cat /tmp/junk
215 matz
216 % ruby -p -i.bak -e '$_.upcase!' /tmp/junk
217 % cat /tmp/junk
218 MATZ
219 % cat /tmp/junk.bak
220 matz
221
222 -l (The lowercase letter “ell”.) Enables automatic line-end‐
223 ing processing, which means to firstly set $\ to the value
224 of $/, and secondly chops every line read using chop!.
225
226 -n Causes Ruby to assume the following loop around your
227 script, which makes it iterate over file name arguments
228 somewhat like sed -n or awk.
229
230 while gets
231 ...
232 end
233
234 -p Acts mostly same as -n switch, but print the value of
235 variable $_ at the each end of the loop. For example:
236
237 % echo matz | ruby -p -e '$_.tr! "a-z", "A-Z"'
238 MATZ
239
240 -r library Causes Ruby to load the library using require. It is use‐
241 ful when using -n or -p.
242
243 -s Enables some switch parsing for switches after script name
244 but before any file name arguments (or before a --). Any
245 switches found there are removed from ARGV and set the
246 corresponding variable in the script. For example:
247
248 #! /usr/local/bin/ruby -s
249 # prints "true" if invoked with `-xyz' switch.
250 print "true\n" if $xyz
251
252 On some systems $0 does not always contain the full path‐
253 name, so you need the -S switch to tell Ruby to search for
254 the script if necessary (to handle embedded spaces and
255 such). A better construct than $* would be ${1+"$@"}, but
256 it does not work if the script is being interpreted by
257 csh(1).
258
259 -v Enables verbose mode. Ruby will print its version at the
260 beginning and set the variable $VERBOSE to true. Some
261 methods print extra messages if this variable is true. If
262 this switch is given, and no other switches are present,
263 Ruby quits after printing its version.
264
265 -w Enables verbose mode without printing version message at
266 the beginning. It sets the $VERBOSE variable to true.
267
268 -x[directory] Tells Ruby that the script is embedded in a message.
269 Leading garbage will be discarded until the first line
270 that starts with “#!” and contains the string, “ruby”.
271 Any meaningful switches on that line will be applied. The
272 end of the script must be specified with either EOF, ^D
273 (control-D), ^Z (control-Z), or the reserved word __END__.
274 If the directory name is specified, Ruby will switch to
275 that directory before executing script.
276
277 -y
278 --yydebug DO NOT USE.
279
280 Turns on compiler debug mode. Ruby will print a bunch of
281 internal state messages during compilation. Only specify
282 this switch you are going to debug the Ruby interpreter.
283
284 --disable-FEATURE
285 --enable-FEATURE
286 Disables (or enables) the specified FEATURE.
287 --disable-gems
288 --enable-gems Disables (or enables) RubyGems
289 libraries. By default, Ruby will load
290 the latest version of each installed
291 gem. The Gem constant is true if
292 RubyGems is enabled, false if other‐
293 wise.
294
295 --disable-rubyopt
296 --enable-rubyopt Ignores (or considers) the RUBYOPT
297 environment variable. By default, Ruby
298 considers the variable.
299
300 --disable-all
301 --enable-all Disables (or enables) all features.
302
303 --dump=target DO NOT USE.
304
305 Prints the specified target. target can be one of;
306
307 insns disassembled instructions
308
309 Only specify this switch if you are going to debug the
310 Ruby interpreter.
311
312 --verbose Enables verbose mode without printing version message at
313 the beginning. It sets the $VERBOSE variable to true. If
314 this switch is given, and no other switches are present,
315 Ruby quits after printing its version.
316
318 RUBYLIB A colon-separated list of directories that are added to
319 Ruby's library load path ($:). Directories from this
320 environment variable are searched before the standard
321 load path is searched.
322
323 e.g.:
324 RUBYLIB="$HOME/lib/ruby:$HOME/lib/rubyext"
325
326 RUBYOPT Additional Ruby options.
327
328 e.g.
329 RUBYOPT="-w -Ke"
330
331 Note that RUBYOPT can contain only -d, -E, -I, -K, -r,
332 -T, -U, -v, -w, -W, --debug, --disable-FEATURE and
333 --enable-FEATURE.
334
335 RUBYPATH A colon-separated list of directories that Ruby searches
336 for Ruby programs when the -S flag is specified. This
337 variable precedes the PATH environment variable.
338
339 RUBYSHELL The path to the system shell command. This environment
340 variable is enabled for only mswin32, mingw32, and OS/2
341 platforms. If this variable is not defined, Ruby refers
342 to COMSPEC.
343
344 PATH Ruby refers to the PATH environment variable on calling
345 Kernel#system.
346
347 RUBYLIB_PREFIX This variable is obsolete.
348
349 And Ruby depends on some RubyGems related environment variables unless
350 RubyGems is disabled. See the help of gem(1) as bellow.
351
352 % gem help
353
355 http://www.ruby-lang.org The official web site.
356 http://www.rubyforge.org hosting many open source ruby projects.
357 http://raa.ruby-lang.org Ruby Application Archive.
358
360 Security vulnerabilities should be reported via an email to
361 ⟨security@ruby-lang.org⟩. Reported problems will be published after
362 they've been fixed.
363
364 And you can report other bugs and feature requests via the Ruby Issue
365 Tracking System (http://bugs.ruby-lang.org). Do not report security vul‐
366 nerabilities via the system because it publishes the vulnerabilities
367 immediately.
368
370 Ruby is designed and implemented by Yukihiro Matsumoto <matz@netlab.jp>.
371
372 See <http://bugs.ruby-lang.org/wiki/ruby/Contributors> for contributors
373 to Ruby.
374
375UNIX November 7, 2012 UNIX