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