1 .\"Ruby is copyrighted by Yukihiro Matsumoto <matz@netlab.jp>.
3 .Dt RUBY \&1 "Ruby Programmer's Reference Guide"
7 .Nd Interpreted object-oriented scripting language
13 .Op Fl 0 Ns Op Ar octal
15 .Op Fl E Ar external Ns Op : Ns Ar internal
16 .Op Fl F Ns Op Ar pattern
19 .Op Fl T Ns Op Ar level
20 .Op Fl W Ns Op Ar level
22 .Op Fl i Ns Op Ar extension
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
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.
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
42 Ruby's features are as follows:
45 Ruby is an interpreted language, so you don't have to recompile
46 programs written in Ruby to execute them.
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
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.
57 .It Sy "Simple syntax"
58 Ruby has a simple syntax influenced slightly from Eiffel.
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.
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.
69 .It Sy "Class, inheritance, and methods"
70 Being an object-oriented language, Ruby naturally has basic
71 features like classes, inheritance, and methods.
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
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
87 Ruby has iterators for loop abstraction.
90 In Ruby, you can objectify the procedure.
92 .It Sy "Text processing and regular expressions"
93 Ruby has a bunch of text processing features like in Perl.
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.
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"
111 .It Sy "Direct access to the OS"
114 system calls, often used in system programming.
116 .It Sy "Dynamic loading"
119 systems, you can load object files into the Ruby interpreter
121 .It Sy "Rich libraries"
123 .Dq builtin libraries
125 .Dq standard libraries
126 that are bundled with Ruby, a vast amount of third-party libraries
128 are available via the package management system called
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.
140 The Ruby interpreter accepts the following command-line options (switches).
141 They are quite similar to those of
143 .Bl -tag -width "1234567890123" -compact
146 Prints the copyright notice, and quits immediately without running any
150 Prints the version of the Ruby interpreter, and quits immediately without
153 .It Fl 0 Ns Op Ar octal
156 Specifies the input record separator
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.
161 turns Ruby into paragraph mode.
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
182 Specifies input field separator
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
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.
195 .Bl -hang -offset indent
212 environment variable to search for script, unless its name begins
213 with a slash. This is used to emulate
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 $*
224 does not always contain the full pathname, so you need the
226 switch to tell Ruby to search for the script if necessary (to handle embedded
227 spaces and such). A better construct than
231 but it does not work if the script is being interpreted by
234 .It Fl T Ns Op Ar level=1
235 Turns on taint checks at the specified level (default 1).
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
246 Verbose mode is "silence". It sets the
251 Verbose mode is "medium". It sets the
256 Verbose mode is "verbose". It sets the
266 Turns on auto-split mode when used with
270 In auto-split mode, Ruby executes
272 at beginning of each loop.
275 Causes Ruby to check the syntax of the script and exit without
276 executing. If there are no syntax errors, Ruby will print
278 to the standard output.
287 Specifies script from command-line while telling Ruby not to search
288 the rest of the arguments for a script file name.
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
301 % ruby -p -i.bak -e '$_.upcase!' /tmp/junk
309 (The lowercase letter
311 Enables automatic line-ending processing, which means to firstly set
315 and secondly chops every line read using
319 Causes Ruby to assume the following loop around your script, which
320 makes it iterate over file name arguments somewhat like
325 .Bd -literal -offset indent
332 Acts mostly same as -n switch, but print the value of variable
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"'
341 Causes Ruby to load the library using require. It is useful when using
347 Enables some switch parsing for switches after script name but before
348 any file name arguments (or before a
350 Any switches found there are removed from
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
360 Enables verbose mode. Ruby will print its version at the beginning
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.
368 Enables verbose mode without printing version message at the
369 beginning. It sets the
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
377 and contains the string,
379 Any meaningful switches on that line will be applied. The end of the script
380 must be specified with either
382 .Li "^D" ( Li "control-D" ) ,
383 .Li "^Z" ( Li "control-Z" ) ,
386 If the directory name is specified, Ruby will switch to that directory
387 before executing script.
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
401 .Bl -tag -width "--disable-rubyopt" -compact
404 Disables (or enables) RubyGems libraries. By default, Ruby will load the latest
405 version of each installed gem. The
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
413 environment variable. By default, Ruby considers the variable.
417 Disables (or enables) all features.
421 .It Fl -dump Ns = Ns Ar target
422 Dump some information.
424 Prints the specified target.
427 .Bl -hang -offset indent
429 version description same as
432 brief usage message same as
435 Show long help message same as
438 check of syntax same as
442 compiler debug mode, same as
445 Only specify this switch if you are going to debug the Ruby interpreter.
447 .It Sy parsetree_with_comment
450 Only specify this switch if you are going to debug the Ruby interpreter.
452 disassembled instructions
454 Only specify this switch if you are going to debug the Ruby interpreter.
458 Enables verbose mode without printing version message at the
459 beginning. It sets the
462 If this switch is given, and no script arguments (script file or
464 options) are present, Ruby quits immediately.
468 .Bl -tag -width "RUBYSHELL" -compact
470 A colon-separated list of directories that are added to Ruby's
472 .Pf ( Li "$:" ) . Directories from this environment variable are searched
473 before the standard load path is searched.
476 .Dl RUBYLIB="$HOME/lib/ruby:$HOME/lib/rubyext"
479 Additional Ruby options.
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 .
491 A colon-separated list of directories that Ruby searches for
492 Ruby programs when the
494 flag is specified. This variable precedes the
496 environment variable.
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
507 environment variable on calling Kernel#system.
510 And Ruby depends on some RubyGems related environment variables unless RubyGems is disabled.
514 .Bd -literal -offset indent
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 (
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
532 Reaching the object limit.
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
540 .Bl -hang -offset indent
542 Reaching the old object limit.
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
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.
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
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
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
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
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.
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.
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.