* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / man / ruby.1
blob944844b63949b933cd7ed36a7c6768d6703aa96d
1 .\"Ruby is copyrighted by Yukihiro Matsumoto <matz@netlab.jp>.
2 .Dd April 14, 2018
3 .Dt RUBY \&1 "Ruby Programmer's Reference Guide"
4 .Os UNIX
5 .Sh NAME
6 .Nm ruby
7 .Nd Interpreted object-oriented scripting language
8 .Sh SYNOPSIS
9 .Nm
10 .Op Fl -copyright
11 .Op Fl -version
12 .Op Fl SUacdlnpswvy
13 .Op Fl 0 Ns Op Ar octal
14 .Op Fl C Ar directory
15 .Op Fl E Ar external Ns Op : Ns Ar internal
16 .Op Fl F Ns Op Ar pattern
17 .Op Fl I Ar directory
18 .Op Fl K Ns Op Ar c
19 .Op Fl T Ns Op Ar level
20 .Op Fl W Ns Op Ar level
21 .Op Fl e Ar command
22 .Op Fl i Ns Op Ar extension
23 .Op Fl r Ar library
24 .Op Fl x Ns Op Ar directory
25 .Op Fl - Ns Bro Cm enable Ns | Ns Cm disable Brc Ns - Ns Ar FEATURE
26 .Op Fl -dump Ns = Ns Ar target
27 .Op Fl -verbose
28 .Op Fl -
29 .Op Ar program_file
30 .Op Ar argument ...
31 .Sh DESCRIPTION
32 Ruby is an interpreted scripting language for quick and easy
33 object-oriented programming.  It has many features to process text
34 files and to do system management tasks (like in Perl).  It is simple,
35 straight-forward, and extensible.
36 .Pp
37 If you want a language for easy object-oriented programming, or you
38 don't like the Perl ugliness, or you do like the concept of LISP, but
39 don't like too many parentheses, Ruby might be your language of
40 choice.
41 .Sh FEATURES
42 Ruby's features are as follows:
43 .Bl -tag -width 6n
44 .It Sy "Interpretive"
45 Ruby is an interpreted language, so you don't have to recompile
46 programs written in Ruby to execute them.
47 .Pp
48 .It Sy "Variables have no type (dynamic typing)"
49 Variables in Ruby can contain data of any type.  You don't have to
50 worry about variable typing.  Consequently, it has a weaker compile
51 time check.
52 .Pp
53 .It Sy "No declaration needed"
54 You can use variables in your Ruby programs without any declarations.
55 Variable names denote their scope - global, class, instance, or local.
56 .Pp
57 .It Sy "Simple syntax"
58 Ruby has a simple syntax influenced slightly from Eiffel.
59 .Pp
60 .It Sy "No user-level memory management"
61 Ruby has automatic memory management.  Objects no longer referenced
62 from anywhere are automatically collected by the garbage collector
63 built into the interpreter.
64 .Pp
65 .It Sy "Everything is an object"
66 Ruby is a purely object-oriented language, and was so since its
67 creation.  Even such basic data as integers are seen as objects.
68 .Pp
69 .It Sy "Class, inheritance, and methods"
70 Being an object-oriented language, Ruby naturally has basic
71 features like classes, inheritance, and methods.
72 .Pp
73 .It Sy "Singleton methods"
74 Ruby has the ability to define methods for certain objects.  For
75 example, you can define a press-button action for certain widget by
76 defining a singleton method for the button.  Or, you can make up your
77 own prototype based object system using singleton methods, if you want
78 to.
79 .Pp
80 .It Sy "Mix-in by modules"
81 Ruby intentionally does not have the multiple inheritance as it is a
82 source of confusion.  Instead, Ruby has the ability to share
83 implementations across the inheritance tree.  This is often called a
84 .Sq Mix-in .
85 .Pp
86 .It Sy "Iterators"
87 Ruby has iterators for loop abstraction.
88 .Pp
89 .It Sy "Closures"
90 In Ruby, you can objectify the procedure.
91 .Pp
92 .It Sy "Text processing and regular expressions"
93 Ruby has a bunch of text processing features like in Perl.
94 .Pp
95 .It Sy "M17N, character set independent"
96 Ruby supports multilingualized programming. Easy to process texts
97 written in many different natural languages and encoded in many
98 different character encodings, without dependence on Unicode.
99 .Pp
100 .It Sy "Bignums"
101 With built-in bignums, you can for example calculate factorial(400).
103 .It Sy "Reflection and domain specific languages"
104 Class is also an instance of the Class class. Definition of classes and methods
105 is an expression just as 1+1 is. So your programs can even write and modify programs.
106 Thus you can write your application in your own programming language on top of Ruby.
108 .It Sy "Exception handling"
109 As in Java(tm).
111 .It Sy "Direct access to the OS"
112 Ruby can use most
114 system calls, often used in system programming.
116 .It Sy "Dynamic loading"
117 On most
119 systems, you can load object files into the Ruby interpreter
120 on-the-fly.
121 .It Sy "Rich libraries"
122 In addition to the
123 .Dq builtin libraries
125 .Dq standard libraries
126 that are bundled with Ruby, a vast amount of third-party libraries
127 .Pq Dq gems
128 are available via the package management system called
129 .Sq RubyGems ,
130 namely the
131 .Xr gem 1
132 command.  Visit RubyGems.org
133 .Pq Lk https://rubygems.org/
134 to find the gems you need, and explore GitHub
135 .Pq Lk https://github.com/
136 to see how they are being developed and used.
139 .Sh OPTIONS
140 The Ruby interpreter accepts the following command-line options (switches).
141 They are quite similar to those of
142 .Xr perl 1 .
143 .Bl -tag -width "1234567890123" -compact
145 .It Fl -copyright
146 Prints the copyright notice, and quits immediately without running any
147 script.
149 .It Fl -version
150 Prints the version of the Ruby interpreter, and quits immediately without
151 running any script.
153 .It Fl 0 Ns Op Ar octal
154 (The digit
155 .Dq zero . )
156 Specifies the input record separator
157 .Pf ( Li "$/" )
158 as an octal number. If no digit is given, the null character is taken
159 as the separator.  Other switches may follow the digits.
160 .Fl 00
161 turns Ruby into paragraph mode.
162 .Fl 0777
163 makes Ruby read whole file at once as a single string since there is
164 no legal character with that value.
166 .It Fl C Ar directory
167 .It Fl X Ar directory
168 Causes Ruby to switch to the directory.
170 .It Fl E Ar external Ns Op : Ns Ar internal
171 .It Fl -encoding Ar external Ns Op : Ns Ar internal
172 Specifies the default value(s) for external encodings and internal encoding. Values should be separated with colon (:).
174 You can omit the one for internal encodings, then the value
175 .Pf ( Li "Encoding.default_internal" ) will be nil.
177 .It Fl -external-encoding Ns = Ns Ar encoding
178 .It Fl -internal-encoding Ns = Ns Ar encoding
179 Specify the default external or internal character encoding
181 .It Fl F Ar pattern
182 Specifies input field separator
183 .Pf ( Li "$;" ) .
185 .It Fl I Ar directory
186 Used to tell Ruby where to load the library scripts.  Directory path
187 will be added to the load-path variable
188 .Pf ( Li "$:" ) .
190 .It Fl K Ar kcode
191 Specifies KANJI (Japanese) encoding. The default value for script encodings
192 .Pf ( Li "__ENCODING__" ) and external encodings ( Li "Encoding.default_external" ) will be the specified one.
193 .Ar kcode
194 can be one of
195 .Bl -hang -offset indent
196 .It Sy e
197 EUC-JP
199 .It Sy s
200 Windows-31J (CP932)
202 .It Sy u
203 UTF-8
205 .It Sy n
206 ASCII-8BIT (BINARY)
209 .It Fl S
210 Makes Ruby use the
211 .Ev PATH
212 environment variable to search for script, unless its name begins
213 with a slash.  This is used to emulate
214 .Li #!
215 on machines that don't support it, in the following manner:
216 .Bd -literal -offset indent
217 #! /usr/local/bin/ruby
218 # This line makes the next one a comment in Ruby \e
219   exec /usr/local/bin/ruby -S $0 $*
222 On some systems
223 .Li "$0"
224 does not always contain the full pathname, so you need the
225 .Fl S
226 switch to tell Ruby to search for the script if necessary (to handle embedded
227 spaces and such).  A better construct than
228 .Li "$*"
229 would be
230 .Li ${1+"$@"} ,
231 but it does not work if the script is being interpreted by
232 .Xr csh 1 .
234 .It Fl T Ns Op Ar level=1
235 Turns on taint checks at the specified level (default 1).
237 .It Fl U
238 Sets the default value for internal encodings
239 .Pf ( Li "Encoding.default_internal" ) to UTF-8.
241 .It Fl W Ns Op Ar level=2
242 Turns on verbose mode at the specified level without printing the version
243 message at the beginning. The level can be;
244 .Bl -hang -offset indent
245 .It Sy 0
246 Verbose mode is "silence". It sets the
247 .Li "$VERBOSE"
248 to nil.
250 .It Sy 1
251 Verbose mode is "medium". It sets the
252 .Li "$VERBOSE"
253 to false.
255 .It Sy 2 (default)
256 Verbose mode is "verbose". It sets the
257 .Li "$VERBOSE"
258 to true.
259 .Fl W Ns
260 2 is the same as
261 .Fl w
265 .It Fl a
266 Turns on auto-split mode when used with
267 .Fl n
269 .Fl p .
270 In auto-split mode, Ruby executes
271 .Dl $F = $_.split
272 at beginning of each loop.
274 .It Fl c
275 Causes Ruby to check the syntax of the script and exit without
276 executing. If there are no syntax errors, Ruby will print
277 .Dq Syntax OK
278 to the standard output.
280 .It Fl d
281 .It Fl -debug
282 Turns on debug mode.
283 .Li "$DEBUG"
284 will be set to true.
286 .It Fl e Ar command
287 Specifies script from command-line while telling Ruby not to search
288 the rest of the arguments for a script file name.
290 .It Fl h
291 .It Fl -help
292 Prints a summary of the options.
294 .It Fl i Ar extension
295 Specifies in-place-edit mode.  The extension, if specified, is added
296 to old file name to make a backup copy.  For example:
297 .Bd -literal -offset indent
298 % echo matz > /tmp/junk
299 % cat /tmp/junk
300 matz
301 % ruby -p -i.bak -e '$_.upcase!' /tmp/junk
302 % cat /tmp/junk
303 MATZ
304 % cat /tmp/junk.bak
305 matz
308 .It Fl l
309 (The lowercase letter
310 .Dq ell . )
311 Enables automatic line-ending processing, which means to firstly set
312 .Li "$\e"
313 to the value of
314 .Li "$/" ,
315 and secondly chops every line read using
316 .Li chomp! .
318 .It Fl n
319 Causes Ruby to assume the following loop around your script, which
320 makes it iterate over file name arguments somewhat like
321 .Nm sed
322 .Fl n
324 .Nm awk .
325 .Bd -literal -offset indent
326 while gets
327   ...
331 .It Fl p
332 Acts mostly same as -n switch, but print the value of variable
333 .Li "$_"
334 at the each end of the loop.  For example:
335 .Bd -literal -offset indent
336 % echo matz | ruby -p -e '$_.tr! "a-z", "A-Z"'
337 MATZ
340 .It Fl r Ar library
341 Causes Ruby to load the library using require.  It is useful when using
342 .Fl n
344 .Fl p .
346 .It Fl s
347 Enables some switch parsing for switches after script name but before
348 any file name arguments (or before a
349 .Fl - ) .
350 Any switches found there are removed from
351 .Li ARGV
352 and set the corresponding variable in the script.  For example:
353 .Bd -literal -offset indent
354 #! /usr/local/bin/ruby -s
355 # prints "true" if invoked with `-xyz' switch.
356 print "true\en" if $xyz
359 .It Fl v
360 Enables verbose mode.  Ruby will print its version at the beginning
361 and set the variable
362 .Li "$VERBOSE"
363 to true.  Some methods print extra messages if this variable is true.
364 If this switch is given, and no other switches are present, Ruby quits
365 after printing its version.
367 .It Fl w
368 Enables verbose mode without printing version message at the
369 beginning.  It sets the
370 .Li "$VERBOSE"
371 variable to true.
373 .It Fl x Ns Op Ar directory
374 Tells Ruby that the script is embedded in a message.  Leading garbage
375 will be discarded until the first line that starts with
376 .Dq #!
377 and contains the string,
378 .Dq ruby .
379 Any meaningful switches on that line will be applied.  The end of the script
380 must be specified with either
381 .Li EOF ,
382 .Li "^D" ( Li "control-D" ) ,
383 .Li "^Z" ( Li "control-Z" ) ,
384 or the reserved word
385 .Li __END__ .
386 If the directory name is specified, Ruby will switch to that directory
387 before executing script.
389 .It Fl y
390 .It Fl -yydebug
391 DO NOT USE.
393 Turns on compiler debug mode.  Ruby will print a bunch of internal
394 state messages during compilation.  Only specify this switch you are going to
395 debug the Ruby interpreter.
397 .It Fl -disable- Ns Ar FEATURE
398 .It Fl -enable- Ns Ar FEATURE
399 Disables (or enables) the specified
400 .Ar FEATURE .
401 .Bl -tag -width "--disable-rubyopt" -compact
402 .It Fl -disable-gems
403 .It Fl -enable-gems
404 Disables (or enables) RubyGems libraries.  By default, Ruby will load the latest
405 version of each installed gem. The
406 .Li Gem
407 constant is true if RubyGems is enabled, false if otherwise.
409 .It Fl -disable-rubyopt
410 .It Fl -enable-rubyopt
411 Ignores (or considers) the
412 .Ev RUBYOPT
413 environment variable. By default, Ruby considers the variable.
415 .It Fl -disable-all
416 .It Fl -enable-all
417 Disables (or enables) all features.
421 .It Fl -dump Ns = Ns Ar target
422 Dump some information.
424 Prints the specified target.
425 .Ar target
426 can be one of;
427 .Bl -hang -offset indent
428 .It Sy version
429 version description same as
430 .Fl -version
431 .It Sy usage
432 brief usage message same as
433 .Fl h
434 .It Sy help
435 Show long help message same as
436 .Fl -help
437 .It Sy syntax
438 check of syntax same as
439 .Fl c
440 .Fl -yydebug
441 .It Sy yydebug
442 compiler debug mode, same as
443 .Fl -yydebug
445 Only specify this switch if you are going to debug the Ruby interpreter.
446 .It Sy parsetree
447 .It Sy parsetree_with_comment
448 AST nodes tree
450 Only specify this switch if you are going to debug the Ruby interpreter.
451 .It Sy insns
452 disassembled instructions
454 Only specify this switch if you are going to debug the Ruby interpreter.
457 .It Fl -verbose
458 Enables verbose mode without printing version message at the
459 beginning.  It sets the
460 .Li "$VERBOSE"
461 variable to true.
462 If this switch is given, and no script arguments (script file or
463 .Fl e
464 options) are present, Ruby quits immediately.
467 .Sh ENVIRONMENT
468 .Bl -tag -width "RUBYSHELL" -compact
469 .It Ev RUBYLIB
470 A colon-separated list of directories that are added to Ruby's
471 library load path
472 .Pf ( Li "$:" ) . Directories from this environment variable are searched
473 before the standard load path is searched.
475 e.g.:
476 .Dl RUBYLIB="$HOME/lib/ruby:$HOME/lib/rubyext"
478 .It Ev RUBYOPT
479 Additional Ruby options.
481 e.g.
482 .Dl RUBYOPT="-w -Ke"
484 Note that RUBYOPT can contain only
485 .Fl d , Fl E , Fl I , Fl K , Fl r , Fl T , Fl U , Fl v , Fl w , Fl W, Fl -debug ,
486 .Fl -disable- Ns Ar FEATURE
488 .Fl -enable- Ns Ar FEATURE .
490 .It Ev RUBYPATH
491 A colon-separated list of directories that Ruby searches for
492 Ruby programs when the
493 .Fl S
494 flag is specified.  This variable precedes the
495 .Ev PATH
496 environment variable.
498 .It Ev RUBYSHELL
499 The path to the system shell command.  This environment variable is
500 enabled for only mswin32, mingw32, and OS/2 platforms.  If this
501 variable is not defined, Ruby refers to
502 .Ev COMSPEC .
504 .It Ev PATH
505 Ruby refers to the
506 .Ev PATH
507 environment variable on calling Kernel#system.
510 And Ruby depends on some RubyGems related environment variables unless RubyGems is disabled.
511 See the help of
512 .Xr gem 1
513 as below.
514 .Bd -literal -offset indent
515 % gem help
518 .Sh GC ENVIRONMENT
519 The Ruby garbage collector (GC) tracks objects in fixed-sized slots,
520 but each object may have auxiliary memory allocations handled by the
521 malloc family of C standard library calls (
522 .Xr malloc 3 ,
523 .Xr calloc 3 ,
525 .Xr realloc 3 ) .
526 In this documentatation, the "heap" refers to the Ruby object heap
527 of fixed-sized slots, while "malloc" refers to auxiliary
528 allocations commonly referred to as the "process heap".
529 Thus there are at least two possible ways to trigger GC:
530 .Bl -hang -offset indent
531 .It Sy 1
532 Reaching the object limit.
533 .It Sy 2
534 Reaching the malloc limit.
537 In Ruby 2.1, the generational GC was introduced and the limits are divided
538 into young and old generations, providing two additional ways to trigger
539 a GC:
540 .Bl -hang -offset indent
541 .It Sy 3
542 Reaching the old object limit.
543 .It Sy 4
544 Reaching the old malloc limit.
547 There are currently 4 possible areas where the GC may be tuned by
548 the following 11 environment variables:
549 .Bl -hang -compact -width "RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR"
550 .It Ev RUBY_GC_HEAP_INIT_SLOTS
551 Initial allocation slots.  Introduced in Ruby 2.1, default: 10000.
553 .It Ev RUBY_GC_HEAP_FREE_SLOTS
554 Prepare at least this amount of slots after GC.
555 Allocate this number slots if there are not enough slots.
556 Introduced in Ruby 2.1, default: 4096
558 .It Ev RUBY_GC_HEAP_GROWTH_FACTOR
559 Increase allocation rate of heap slots by this factor.
560 Introduced in Ruby 2.1, default: 1.8, minimum: 1.0 (no growth)
562 .It Ev RUBY_GC_HEAP_GROWTH_MAX_SLOTS
563 Allocation rate is limited to this number of slots,
564 preventing excessive allocation due to RUBY_GC_HEAP_GROWTH_FACTOR.
565 Introduced in Ruby 2.1, default: 0 (no limit)
567 .It Ev RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR
568 Perform a full GC when the number of old objects is more than R * N,
569 where R is this factor and N is the number of old objects after the
570 last full GC.
571 Introduced in Ruby 2.1.1, default: 2.0
573 .It Ev RUBY_GC_MALLOC_LIMIT
574 The initial limit of young generation allocation from the malloc-family.
575 GC will start when this limit is reached.
576 Default: 16MB
578 .It Ev RUBY_GC_MALLOC_LIMIT_MAX
579 The maximum limit of young generation allocation from malloc before GC starts.
580 Prevents excessive malloc growth due to RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR.
581 Introduced in Ruby 2.1, default: 32MB.
583 .It Ev RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR
584 Increases the limit of young generation malloc calls, reducing
585 GC frequency but increasing malloc growth until RUBY_GC_MALLOC_LIMIT_MAX
586 is reached.
587 Introduced in Ruby 2.1, default: 1.4, minimum: 1.0 (no growth)
589 .It Ev RUBY_GC_OLDMALLOC_LIMIT
590 The initial limit of old generation allocation from malloc,
591 a full GC will start when this limit is reached.
592 Introduced in Ruby 2.1, default: 16MB
594 .It Ev RUBY_GC_OLDMALLOC_LIMIT_MAX
595 The maximum limit of old generation allocation from malloc before a
596 full GC starts.
597 Prevents excessive malloc growth due to RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR.
598 Introduced in Ruby 2.1, default: 128MB
600 .It Ev RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR
601 Increases the limit of old generation malloc allocation, reducing full
602 GC frequency but increasing malloc growth until RUBY_GC_OLDMALLOC_LIMIT_MAX
603 is reached.
604 Introduced in Ruby 2.1, default: 1.2, minimum: 1.0 (no growth)
607 .Sh STACK SIZE ENVIRONMENT
608 Stack size environment variables are implementation-dependent and
609 subject to change with different versions of Ruby.  The VM stack is used
610 for pure-Ruby code and managed by the virtual machine.  Machine stack is
611 used by the operating system and its usage is dependent on C extensions
612 as well as C compiler options.  Using lower values for these may allow
613 applications to keep more Fibers or Threads running; but increases the
614 chance of SystemStackError exceptions and segmentation faults (SIGSEGV).
615 These environment variables are available since Ruby 2.0.0.
616 All values are specified in bytes.
618 .Bl -hang -compact -width "RUBY_THREAD_MACHINE_STACK_SIZE"
619 .It Ev RUBY_THREAD_VM_STACK_SIZE
620 VM stack size used at thread creation.
621 default: 524288 (32-bit CPU) or 1048575 (64-bit)
623 .It Ev RUBY_THREAD_MACHINE_STACK_SIZE
624 Machine stack size used at thread creation.
625 default: 524288 or 1048575
627 .It Ev RUBY_FIBER_VM_STACK_SIZE
628 VM stack size used at fiber creation.
629 default: 65536 or 131072
631 .It Ev RUBY_FIBER_MACHINE_STACK_SIZE
632 Machine stack size used at fiber creation.
633 default: 262144 or 524288
636 .Sh SEE ALSO
637 .Bl -hang -compact -width "https://www.ruby-toolbox.com/"
638 .It Lk https://www.ruby-lang.org/
639 The official web site.
640 .It Lk https://www.ruby-toolbox.com/
641 Comprehensive catalog of Ruby libraries.
644 .Sh REPORTING BUGS
645 .Bl -bullet
647 Security vulnerabilities should be reported via an email to
648 .Mt security@ruby-lang.org .
649 Reported problems will be published after being fixed.
652 Other bugs and feature requests can be reported via the
653 Ruby Issue Tracking System
654 .Pq Lk https://bugs.ruby-lang.org/ .
655 Do not report security vulnerabilities
656 via this system because it publishes the vulnerabilities immediately.
658 .Sh AUTHORS
659 Ruby is designed and implemented by
660 .An Yukihiro Matsumoto Aq matz@netlab.jp .
663 .Aq Lk https://bugs.ruby-lang.org/projects/ruby/wiki/Contributors
664 for contributors to Ruby.