1RUBY(1) UNIX RUBY(1)
2
3
4
6 ruby - Interpreted object-oriented scripting language
7
9 ruby [--copyright] [--version] [-Sacdlnpswvy] [-0[octal]] [-C direc‐
10 tory] [-F pattern] [-I directory] [-K c] [-T[level]] [-e command]
11 [-i[extension]] [-r library] [-x[directory]] [--] [program_file] [argu‐
12 ment ...]
13
15 Ruby is an interpreted scripting language for quick and easy object-
16 oriented programming. It has many features to process text files and
17 to do system management tasks (as in Perl). It is simple, straight-
18 forward, and extensible.
19
20 If you want a language for easy object-oriented programming, or you
21 don't like the Perl ugliness, or you do like the concept of LISP, but
22 don't like too much parentheses, Ruby may be the language of your
23 choice.
24
26 Ruby's features are as follows:
27
28 Interpretive
29 Ruby is an interpreted language, so you don't have to recompile
30 programs written in Ruby to execute them.
31
32
33 Variables have no type (dynamic typing)
34 Variables in Ruby can contain data of any type. You don't have
35 to worry about variable typing. Consequently, it has a weaker
36 compile time check.
37
38
39 No declaration needed
40 You can use variables in your Ruby programs without any declara‐
41 tions. Variable names denote their scope, local, global,
42 instance, etc.
43
44
45 Simple syntax
46 Ruby has a simple syntax influenced slightly from Eiffel.
47
48
49 No user-level memory management
50 Ruby has automatic memory management. Objects no longer refer‐
51 enced from anywhere are automatically collected by the garbage
52 collector built into the interpreter.
53
54
55 Everything is an object
56 Ruby is the purely object-oriented language, and was so since
57 its creation. Even such basic data as integers are seen as
58 objects.
59
60
61 Class, inheritance, and methods
62 Of course, as an object-oriented language, Ruby has such basic
63 features like classes, inheritance, and methods.
64
65
66 Singleton methods
67 Ruby has the ability to define methods for certain objects. For
68 example, you can define a press-button action for certain widget
69 by defining a singleton method for the button. Or, you can make
70 up your own prototype based object system using singleton meth‐
71 ods, if you want to.
72
73
74 Mix-in by modules
75 Ruby intentionally does not have the multiple inheritance as it
76 is a source of confusion. Instead, Ruby has the ability to
77 share implementations across the inheritance tree. This is
78 often called `Mix-in'.
79
80
81 Iterators
82 Ruby has iterators for loop abstraction.
83
84
85 Closures
86 In Ruby, you can objectify the procedure.
87
88
89 Text processing and regular expression
90 Ruby has a bunch of text processing features like in Perl.
91
92
93 Bignums
94 With built-in bignums, you can for example calculate facto‐
95 rial(400).
96
97
98 Exception handling
99 As in Java(tm).
100
101
102 Direct access to the OS
103 Ruby can use most UNIX system calls, often used in system pro‐
104 gramming.
105
106
107 Dynamic loading
108 On most UNIX systems, you can load object files into the Ruby
109 interpreter on-the-fly.
110
111
113 Ruby interpreter accepts following command-line options (switches).
114 They are quite similar to those of perl(1).
115
116
117 --copyright
118 Prints the copyright notice.
119
120
121 --version
122 Prints the version of Ruby interpreter.
123
124
125 -0[octal]
126 (The digit ``zero''.) Specifies the input record separator
127 ("$/") as an octal number. If no digit is given, the null char‐
128 acter is taken as the separator. Other switches may follow the
129 digits. -00 turns Ruby into paragraph mode. -0777 makes Ruby
130 read whole file at once as a single string since there is no
131 legal character with that value.
132
133
134 -C directory
135 Causes Ruby to switch to the directory.
136
137
138 -F pattern
139 Specifies input field separator ("$;").
140
141
142 -I directory
143 Used to tell Ruby where to load the library scripts. Directory
144 path will be added to the load-path variable ("$:").
145
146
147 -K kcode
148 Specifies KANJI (Japanese) encoding.
149
150
151 -S Makes Ruby use the PATH environment variable to search for
152 script, unless if its name begins with a slash. This is used to
153 emulate #! on machines that don't support it, in the following
154 manner:
155
156 #! /usr/local/bin/ruby
157 # This line makes the next one a comment in Ruby \
158 exec /usr/local/bin/ruby -S $0 $*
159
160
161 -T[level]
162 Turns on taint checks at the specified level (default 1).
163
164
165 -a Turns on auto-split mode when used with -n or -p. In auto-split
166 mode, Ruby executes
167 $F = $_.split
168 at beginning of each loop.
169
170
171 -c Causes Ruby to check the syntax of the script and exit without
172 executing. If there are no syntax errors, Ruby will print ``Syn‐
173 tax OK'' to the standard output.
174
175
176 -d
177
178 --debug
179 Turns on debug mode. "$DEBUG" will be set to true.
180
181
182 -e command
183 Specifies script from command-line while telling Ruby not to
184 search the rest of arguments for a script file name.
185
186
187 -h
188
189 --help Prints a summary of the options.
190
191
192 -i extension
193 Specifies in-place-edit mode. The extension, if specified, is
194 added to old file name to make a backup copy. For example:
195
196 % echo matz > /tmp/junk
197 % cat /tmp/junk
198 matz
199 % ruby -p -i.bak -e '$_.upcase!' /tmp/junk
200 % cat /tmp/junk
201 MATZ
202 % cat /tmp/junk.bak
203 matz
204
205
206 -l (The lowercase letter ``ell''.) Enables automatic line-ending
207 processing, which means to firstly set "$\" to the value of
208 "$/", and secondly chops every line read using chop!.
209
210
211 -n Causes Ruby to assume the following loop around your script,
212 which makes it iterate over file name arguments somewhat like
213 sed -n or awk.
214
215 while gets
216 ...
217 end
218
219
220 -p Acts mostly same as -n switch, but print the value of variable
221 "$_" at the each end of the loop. For example:
222
223 % echo matz | ruby -p -e '$_.tr! "a-z", "A-Z"'
224 MATZ
225
226
227 -r library
228 Causes Ruby to load the library using require. It is useful
229 when using -n or -p.
230
231
232 -s Enables some switch parsing for switches after script name but
233 before any file name arguments (or before a --). Any switches
234 found there are removed from ARGV and set the corresponding
235 variable in the script. For example:
236
237 #! /usr/local/bin/ruby -s
238 # prints "true" if invoked with `-xyz' switch.
239 print "true\n" if $xyz
240
241 On some systems "$0" does not always contain the full pathname,
242 so you need the -S switch to tell Ruby to search for the script
243 if necessary. To handle embedded spaces or such. A better con‐
244 struct than "$*" would be ${1+"$@"}, but it does not work if the
245 script is being interpreted by csh(1).
246
247
248 -v
249
250 --verbose
251 Enables verbose mode. Ruby will print its version at the begin‐
252 ning, and set the variable "$VERBOSE" to true. Some methods
253 print extra messages if this variable is true. If this switch
254 is given, and no other switches are present, Ruby quits after
255 printing its version.
256
257
258 -w Enables verbose mode without printing version message at the
259 beginning. It sets the "$VERBOSE" variable to true.
260
261
262 -x[directory]
263 Tells Ruby that the script is embedded in a message. Leading
264 garbage will be discarded until the first that starts with
265 ``#!'' and contains the string, ``ruby''. Any meaningful
266 switches on that line will applied. The end of script must be
267 specified with either EOF, "^D" ("control-D"), "^Z" ("control-
268 Z"), or reserved word __END__. If the directory name is speci‐
269 fied, Ruby will switch to that directory before executing
270 script.
271
272
273 -y
274
275 --yydebug
276 Turns on compiler debug mode. Ruby will print a bunch of inter‐
277 nal state messages during compiling scripts. You don't have to
278 specify this switch, unless you are going to debug the Ruby
279 interpreter.
280
281
283 RUBYLIB
284 A colon-separated list of directories that are added to Ruby's
285 library load path ("$:"). Directories from this environment
286 variable are searched before the standard load path is searched.
287
288 e.g.:
289 RUBYLIB="$HOME/lib/ruby:$HOME/lib/rubyext"
290
291
292 RUBYOPT
293 Additional Ruby options.
294
295 e.g.
296 RUBYOPT="-w -Ke"
297
298
299 RUBYPATH
300 A colon-separated list of directories that Ruby searches for
301 Ruby programs when the -S flag is specified. This variable pre‐
302 cedes the PATH environment variable.
303
304
305 RUBYSHELL
306 The path to the system shell command. This environment variable
307 is enabled for only mswin32, mingw32, and OS/2 platforms. If
308 this variable is not defined, Ruby refers to COMSPEC.
309
310
311 PATH Ruby refers to the PATH environment variable on calling Ker‐
312 nel#system.
313
314
315 RUBYLIB_PREFIX
316 This variable is obsolete.
317
318
320 Ruby is designed and implemented by Yukihiro Matsumoto <matz@net‐
321 lab.jp>.
322
323
324
325December 31, 2002 Ruby Programmers Reference Guide RUBY(1)