RUBY(1)

๐Ÿ’– Ruby's Reference Guide (Section 1) ๐Ÿ’–

okay so this is the Ruby Programmer's Reference Guide, manual section 1, and I am so ready to explain it to you ๐Ÿซถ let's get into it bestie โœจ

๐ŸŽ€ NAME ๐ŸŽ€

ruby โ€” Interpreted object-oriented scripting language ๐Ÿฆ‹

๐ŸŒธ SYNOPSIS ๐ŸŒธ

ruby [--copyright] [--version] [-SUacdlnpswvy] [-0[octal]]
     [-C directory] [-E external[:internal]] [-F[pattern]]
     [-I directory] [-K[c]] [-T[level]] [-W[level]] [-e command]
     [-i[extension]] [-r library] [-x[directory]]
     [--{enable|disable}-FEATURE] [--dump=target] [--verbose]
     [--crash-report=template] [--] [program_file] [argument ...]

๐Ÿ”ฅ DESCRIPTION ๐Ÿ”ฅ

okay so Ruby is an interpreted scripting language that's built for quick and easy object-oriented programming ๐Ÿ’… it's got a bunch of features for processing text files and handling system management tasks (kinda like Perl does) โœจ and it's simple, straightforward, and extensible ๐ŸŒˆ

if you want a language for easy OOP, or you're over the Perl ugliness, or you love the concept of LISP but can't handle all those parentheses ๐Ÿ’€ then Ruby might be your language of choice ๐Ÿ“

๐Ÿ‘‘ FEATURES ๐Ÿ‘‘

bestie, Ruby's features are giving everything, so here's the full list โœจ

  • Interpretive ๐Ÿงš Ruby is an interpreted language, so you don't have to recompile your Ruby programs before you run them ๐Ÿซถ
  • Variables have no type (dynamic typing) ๐Ÿฆ‹ Variables in Ruby can hold data of any type, so you don't have to stress about variable typing ๐Ÿ™ˆ the tradeoff is that it has a weaker compile time check ๐Ÿ˜ญ
  • No declaration needed ๐Ÿ’œ you can use variables in your Ruby programs without declaring them first ๐Ÿ’… the variable's name tells you its scope: global, class, instance, or local โœจ
  • Simple syntax ๐ŸŒธ Ruby has a simple syntax, slightly influenced by Eiffel ๐ŸŽ€
  • No user-level memory management ๐Ÿงš Ruby does automatic memory management for you ๐Ÿ™Œ objects that nothing references anymore get collected automatically by the garbage collector built into the interpreter ๐Ÿ—‘๏ธโœจ
  • Everything is an object ๐Ÿ”ฎ Ruby is a purely object-oriented language, and it has been since it was created ๐Ÿ‘‘ even basic stuff like integers counts as an object ๐Ÿคฏ
  • Class, inheritance, and methods ๐Ÿฆ‹ because Ruby is OOP to its core, it naturally has the basics: classes, inheritance, and methods ๐Ÿ’–
  • Singleton methods ๐ŸŒˆ Ruby lets you define methods for certain objects ๐Ÿ”ฅ for example, you can give a specific widget a press-button action by defining a singleton method on that button ๐ŸŽ€ you can also build your own prototype-based object system with singleton methods if you're feeling bold ๐Ÿ’…
  • Mix-in by modules ๐Ÿ’œ Ruby intentionally skips multiple inheritance because it's a major source of confusion ๐Ÿ™ƒ instead, it lets you share implementations across the inheritance tree ๐Ÿซถ this is often called a โ€˜Mix-inโ€™ โœจ
  • Iterators ๐Ÿ“ Ruby has iterators for loop abstraction ๐Ÿ”
  • Closures ๐Ÿ”ฎ in Ruby, you can objectify the procedure ๐Ÿงš
  • Text processing and regular expressions ๐Ÿ“œ Ruby has a whole bunch of text processing features, just like Perl ๐Ÿ’…
  • M17N, character set independent ๐ŸŒ Ruby supports multilingualized programming โœจ it's easy to process texts written in lots of different natural languages and encoded in lots of different character encodings ๐ŸŒธ and it doesn't depend on Unicode ๐Ÿซถ
  • Bignums ๐Ÿ”ข with built-in bignums, you can do things like calculate factorial(400) ๐Ÿคฏ๐Ÿ˜ญ
  • Reflection and domain specific languages ๐Ÿชž Class is also an instance of the Class class ๐ŸคŒ defining classes and methods is an expression, just like 1+1 is ๐Ÿ’… so your programs can even write and modify other programs ๐Ÿฆ‹ which means you can build your application in your own programming language on top of Ruby ๐Ÿ‘‘
  • Exception handling โšก as in Java(tm) ๐Ÿ’€
  • Direct access to the OS ๐Ÿ–ฅ๏ธ Ruby can use most Unix system calls, which are often used in system programming ๐Ÿ”ฅ
  • Dynamic loading ๐ŸŒŠ on most Unix systems, you can load object files into the Ruby interpreter on-the-fly โœจ
  • Rich libraries ๐Ÿ’Ž besides the โ€œbuiltin librariesโ€ and โ€œstandard librariesโ€ that come bundled with Ruby, there's a vast amount of third-party libraries (โ€œgemsโ€) available through the package management system called โ€˜RubyGemsโ€™, which you use with the gem(1) command ๐Ÿ“ฆ visit RubyGems.org (https://rubygems.org/) to find the gems you need ๐Ÿ’– and explore GitHub (https://github.com/) to see how they're being developed and used ๐Ÿซถ๐ŸŒˆ

โœจ OPTIONS โœจ

okay so the Ruby interpreter takes a bunch of command-line switches (aka options) ๐Ÿ’… they're pretty similar to the ones in perl(1), so if you've met perl you'll feel right at home ๐Ÿซถ

  • --copyright โ€” shows the copyright notice and bounces immediately, without running any script ๐Ÿ“œ
  • --version โ€” shows which version of the Ruby interpreter you're on, then quits right away without running anything ๐Ÿ”–
  • -0[octal] โ€” (that's a digit "zero", not the letter o ๐Ÿ™ˆ) sets the input record separator ($/) using an octal number. No digit at all means the null character becomes the separator. other switches can come after the digits too. -00 flips Ruby into paragraph mode ๐Ÿ“„ and -0777 makes Ruby slurp the whole file in as one single string, since no legal character has that value ๐Ÿง 
  • -C directory, -X directory โ€” makes Ruby switch to that directory ๐Ÿ“‚๐Ÿฆ‹
  • -E external[:internal], --encoding external[:internal] โ€” sets the default external and internal encodings ๐ŸŒˆ separate the two with a colon (:). you can leave off the internal one, and then Encoding.default_internal ends up as nil ๐Ÿซ 
  • --external-encoding=encoding, --internal-encoding=encoding โ€” sets the default external or internal character encoding, your call ๐ŸŽ€
  • -F pattern โ€” sets the input field separator ($;) ๐Ÿ“
  • -I directory โ€” tells Ruby where to look for library scripts. that directory gets added to the load-path variable ($:) ๐Ÿงš
  • -K kcode โ€” sets the KANJI (Japanese) encoding โœจ the default for script encodings (__ENCODING__) and external encodings (Encoding.default_external) becomes whatever you pick. kcode can be one of these:
    • e โ€” EUC-JP
    • s โ€” Windows-31J (CP932)
    • u โ€” UTF-8
    • n โ€” ASCII-8BIT (BINARY)
  • -S โ€” makes Ruby hunt for the script using the PATH environment variable, unless its name starts with a slash ๐Ÿ” this is basically how people fake #! on machines that can't do it. it looks like this:
#! /usr/local/bin/ruby
# This line makes the next one a comment in Ruby \
  exec /usr/local/bin/ruby -S $0 $*
โš ๏ธ heads up bestie: on some systems $0 doesn't always hold the full pathname, so you need -S so Ruby can search for the script if it has to (that covers embedded spaces and stuff). a better construct than $* would be ${1+"$@"}, but that one breaks if csh(1) is the thing interpreting the script ๐Ÿ™ƒ
  • -T[level=1] โ€” turns on taint checks at the level you give it (default is 1) ๐Ÿ”’
  • -U โ€” sets the default internal encoding (Encoding.default_internal) to UTF-8 ๐Ÿ’œ
  • -W[level=2] โ€” turns on verbose mode at the level you choose, without printing the version message at the start ๐Ÿ“ข the levels are:
    • 0 โ€” "silence" mode, which sets $VERBOSE to nil ๐Ÿคซ
    • 1 โ€” "medium" mode, which sets $VERBOSE to false ๐Ÿ˜
    • 2 (default) โ€” "verbose" mode, which sets $VERBOSE to true ๐Ÿ“ฃ and -W2 is the exact same thing as -w
  • -a โ€” turns on auto-split mode, but only when you pair it with -n or -p โœ‚๏ธ in auto-split mode, Ruby runs $F = $_.split at the start of each loop
  • --backtrace-limit=num โ€” caps how many lines a backtrace can have at num lines (default is -1, which means no limit) ๐Ÿ“‰
  • -c โ€” checks the syntax of the script and exits without actually running it ๐Ÿง if everything's clean, Ruby prints "Syntax OK" to standard output โœ…
  • -d, --debug โ€” turns on debug mode, and $DEBUG gets set to true ๐Ÿ›
  • -e command โ€” hands Ruby the script straight from the command line, and tells Ruby not to go looking through the other arguments for a script file name ๐Ÿ’ป
  • -h, --help โ€” shows a summary of the options ๐Ÿ†˜
  • -i extension โ€” turns on in-place-edit mode ๐Ÿช„ if you give an extension, it gets tacked onto the old file name to make a backup copy. check this iconic example:
% echo matz > /tmp/junk
% cat /tmp/junk
matz
% ruby -p -i.bak -e '$_.upcase!' /tmp/junk
% cat /tmp/junk
MATZ
% cat /tmp/junk.bak
matz
  • -l โ€” (that's a lowercase letter "ell" ๐Ÿ’€) turns on automatic line-ending processing. it first sets $\ to the value of $/, then chops every line read with chomp! โœ‚๏ธ
  • -n โ€” makes Ruby wrap your script in a loop that goes over the file name arguments, kinda like sed -n or awk ๐Ÿ” it assumes this loop around your code:
while gets
  ...
end
  • -p โ€” acts mostly the same as -n, but it also prints the value of $_ at the end of each loop ๐Ÿ–จ๏ธ like this:
% echo matz | ruby -p -e '$_.tr! "a-z", "A-Z"'
MATZ
  • -r library โ€” loads the library using require ๐Ÿ“š super handy alongside -n or -p
  • -s โ€” turns on some switch parsing for switches that come after the script name but before any file name arguments (or before a --) ๐ŸŽ›๏ธ any switches it finds get removed from ARGV and set the matching variable inside the script. like so:
#! /usr/local/bin/ruby -s
# prints "true" if invoked with `-xyz' switch.
print "true\n" if $xyz
  • -v โ€” turns on verbose mode ๐Ÿ“ฃ Ruby prints its version at the start and sets $VERBOSE to true. some methods print extra messages when that variable is true. if -v is the only switch you give, Ruby quits right after printing its version ๐Ÿ‘‹
  • -w โ€” turns on verbose mode without printing the version message at the start, and sets $VERBOSE to true ๐Ÿ”Š
  • -x[directory] โ€” tells Ruby the script is embedded in a message ๐Ÿ“จ any leading garbage gets tossed until the first line that starts with "#!" and contains the string "ruby". any meaningful switches on that line get applied. the script has to end with EOF, ^D (control-D), ^Z (control-Z), or the reserved word __END__ ๐Ÿ”š if you give a directory name, Ruby switches to that directory before running the script ๐Ÿ“‚

  • -y, --yydebug โ€” โš ๏ธ this option is not guaranteed to be compatible. it turns on compiler debug mode, and Ruby spits out a bunch of internal state messages during compilation ๐ŸŒ€ only use this if you're actually debugging the Ruby interpreter itself ๐Ÿ™…โ€โ™€๏ธ
  • --disable-FEATURE, --enable-FEATURE โ€” disables (or enables) the FEATURE you name ๐ŸŽš๏ธ the features are:
    • --disable-gems, --enable-gems โ€” disables (or enables) RubyGems libraries ๐Ÿ’Ž by default, Ruby loads the latest version of each installed gem. the Gem constant is true if RubyGems is enabled and false if it isn't
    • --disable-rubyopt, --enable-rubyopt โ€” ignores (or considers) the RUBYOPT environment variable ๐ŸŒธ by default, Ruby considers it
    • --disable-all, --enable-all โ€” disables (or enables) every feature at once ๐Ÿงจ
  • --dump=target โ€” dumps some information ๐Ÿ—‘๏ธโœจ it prints the target you pick, and target can be one of these:
    • version โ€” prints the version description (same as --version)
    • usage โ€” prints a brief usage message (same as -h)
    • help โ€” shows the long help message (same as --help)
    • syntax โ€” checks the syntax (same as -c --yydebug)

or one of these, which are meant for debugging the interpreter ๐Ÿงช:

  • yydebug โ€” enables compiler debug mode (same as --yydebug)
  • parsetree โ€” prints a textual representation of the Ruby AST for the program ๐ŸŒณ
  • parsetree_with_comment โ€” prints a textual representation of the Ruby AST for the program, but each node is annotated with the Ruby source code it came from ๐Ÿท๏ธ
  • insns โ€” prints a list of disassembled bytecode instructions ๐Ÿ”ฌ
  • insns_without_opt โ€” prints the list of disassembled bytecode instructions before any optimizations get applied ๐Ÿซฃ
  • --verbose โ€” turns on verbose mode without printing the version message at the start, and sets $VERBOSE to true ๐Ÿ“ข if you give this and no script arguments (a script file or -e options), Ruby quits immediately ๐Ÿƒโ€โ™€๏ธ
  • --crash-report=template โ€” sets the template for the path name where the crash report gets saved ๐Ÿ’ฅ check the RUBY_CRASH_REPORT environment variable for the details ๐Ÿซถ

๐ŸŒˆ ENVIRONMENT ๐ŸŒˆ

okay so the environment variables are where Ruby lets you sneak in your own settings ๐Ÿ’… every one of these is a little knob you can twist, bestie

  • RUBYLIB ๐ŸŽ€ โ€” a colon-separated list of directories that get tacked onto Ruby's library load path ($:). These folders get searched before the standard load path, so they're first in line ๐Ÿ‘‘

e.g.:

RUBYLIB="$HOME/lib/ruby:$HOME/lib/rubyext"
  • RUBYOPT ๐Ÿฆ‹ โ€” extra Ruby options, just for you โœจ

e.g.

RUBYOPT="-w -Ke"

ngl this one is picky. RUBYOPT can only contain -d, -E, -I, -K, -r, -T, -U, -v, -w, -W, --debug, --disable-FEATURE and --enable-FEATURE. Anything else is a no-go ๐Ÿ™…โ€โ™€๏ธ

  • RUBYPATH ๐Ÿงš โ€” a colon-separated list of directories Ruby hunts through for Ruby programs when you use the -S flag. This one goes before PATH in the search order ๐Ÿ’œ
  • RUBYSHELL ๐Ÿš โ€” the path to the system shell command. This one is only enabled on mswin32, mingw32, and OS/2 platforms, so it's a Windows-and-OS/2 kinda thing ๐Ÿ’€ If it isn't defined, Ruby falls back to COMSPEC
  • PATH ๐Ÿ“ โ€” Ruby checks the PATH environment variable when it calls Kernel#system

And Ruby depends on some RubyGems related environment variables unless RubyGems is disabled. For the deets, check the help of gem(1) like so:

% gem help

๐Ÿ—‘๏ธ GC ENVIRONMENT ๐Ÿ—‘๏ธ

okay the garbage collector (GC) is the bestie that cleans up after your objects ๐Ÿงน It tracks objects in fixed-sized slots, but each object can also have extra memory allocated through the malloc family of C standard library calls (malloc(3), calloc(3), and realloc(3)). In this doc, the "heap" means the Ruby object heap of fixed-sized slots, while "malloc" means those auxiliary allocations, aka the "process heap". So there are at least two ways to trigger GC:

  1. Reaching the object limit.
  2. Reaching the malloc limit.

In Ruby 2.1, the generational GC showed up and the limits got split into young and old generations, which gives two more ways to trigger a GC ๐ŸŒธ

  1. Reaching the old object limit.
  2. Reaching the old malloc limit.

There are currently 4 possible areas where the GC can be tuned, using these 11 environment variables ๐ŸŽ›๏ธ

  • RUBY_GC_HEAP_INIT_SLOTS ๐ŸŒฑ โ€” initial allocation slots, applied to all slot sizes. Introduced in Ruby 2.1, default: 10000.
  • RUBY_GC_HEAP_%d_INIT_SLOTS ๐ŸŒฟ โ€” initial allocation of slots in one specific heap. You can find the available heaps in the keys of GC.stat_heap. Introduced in Ruby 3.3.
  • RUBY_GC_HEAP_FREE_SLOTS ๐Ÿซง โ€” keeps at least this many slots ready after a GC, and allocates this many if there aren't enough. Introduced in Ruby 2.1, default: 4096.
  • RUBY_GC_HEAP_GROWTH_FACTOR ๐Ÿ“ˆ โ€” multiplies how fast heap slots get allocated by this factor. Introduced in Ruby 2.1, default: 1.8, minimum: 1.0 (no growth).
  • RUBY_GC_HEAP_GROWTH_MAX_SLOTS ๐Ÿ›‘ โ€” caps the allocation rate at this many slots, so RUBY_GC_HEAP_GROWTH_FACTOR can't go wild. Introduced in Ruby 2.1, default: 0 (no limit).
  • RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR ๐Ÿง“ โ€” runs a full GC when the number of old objects is more than R * N, where R is this factor and N is the number of old objects after the last full GC. Introduced in Ruby 2.1.1, default: 2.0.
  • RUBY_GC_MALLOC_LIMIT ๐Ÿผ โ€” the starting limit of young generation allocation from the malloc family. GC kicks off once this limit is hit. Default: 16MB.
  • RUBY_GC_MALLOC_LIMIT_MAX ๐Ÿงข โ€” the biggest the young generation malloc limit can get before GC starts. It stops malloc from growing out of control because of RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR. Introduced in Ruby 2.1, default: 32MB.
  • RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR ๐ŸŒท โ€” raises the young generation malloc limit, so GC runs less often but malloc grows more, until RUBY_GC_MALLOC_LIMIT_MAX is reached. Introduced in Ruby 2.1, default: 1.4, minimum: 1.0 (no growth).
  • RUBY_GC_OLDMALLOC_LIMIT ๐Ÿ•ฐ๏ธ โ€” the starting limit of old generation allocation from malloc. A full GC starts when this limit is reached. Introduced in Ruby 2.1, default: 16MB.
  • RUBY_GC_OLDMALLOC_LIMIT_MAX ๐Ÿ”๏ธ โ€” the maximum old generation malloc allocation before a full GC starts. It prevents excessive malloc growth caused by RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR. Introduced in Ruby 2.1, default: 128MB.
  • RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR ๐Ÿช„ โ€” raises the old generation malloc limit, so full GCs happen less often but malloc grows more, until RUBY_GC_OLDMALLOC_LIMIT_MAX is reached. Introduced in Ruby 2.1, default: 1.2, minimum: 1.0 (no growth).

๐Ÿงต STACK SIZE ENVIRONMENT ๐Ÿงต

the stack size variables are implementation-dependent and can change between Ruby versions, so don't get too attached ๐Ÿ™ˆ The VM stack handles pure-Ruby code and is managed by the virtual machine. The machine stack is used by the operating system and depends on C extensions and C compiler options. Lower values can let you keep more Fibers or Threads running, but the chance of SystemStackError exceptions and segmentation faults (SIGSEGV) goes up ๐Ÿ˜ญ These variables exist since Ruby 2.0.0, and all values are in bytes.

  • RUBY_THREAD_VM_STACK_SIZE ๐Ÿงถ โ€” VM stack size used when a thread is created. default: 524288 (32-bit CPU) or 1048575 (64-bit)
  • RUBY_THREAD_MACHINE_STACK_SIZE ๐Ÿชก โ€” machine stack size used when a thread is created. default: 524288 or 1048575
  • RUBY_FIBER_VM_STACK_SIZE ๐Ÿฆ„ โ€” VM stack size used when a fiber is created. default: 65536 or 131072
  • RUBY_FIBER_MACHINE_STACK_SIZE ๐Ÿ‰ โ€” machine stack size used when a fiber is created. default: 262144 or 524288

๐Ÿ’ฅ CRASH REPORT ENVIRONMENT ๐Ÿ’ฅ

  • RUBY_CRASH_REPORT ๐Ÿ“ โ€” the template for the path name used to save a crash report. default: none

๐Ÿท๏ธ Naming crash report files

the template can have % specifiers that get swapped out for the following values when a crash report file is made:

  • %% โ€” a single % character.
  • %e โ€” basename of the executable.
  • %E โ€” pathname of the executable, with slashes (/) swapped for exclamation marks (!).
  • %f โ€” basename of the program name, $0.
  • %F โ€” pathname of the program name, $0, with slashes (/) swapped for exclamation marks (!).
  • %p โ€” PID of the dumped process.
  • %t โ€” time of the dump, as seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
  • %NNN โ€” a character code in octal.

a lone % at the end of the template gets dropped from the core filename, and so does any % followed by a character that isn't in the list above ๐Ÿซ  Everything else in the template is taken literally. The template can include / characters, which act as delimiters for directory names ๐Ÿ“

๐Ÿšฐ Piping crash reports to a program

if the first character of this file is a pipe symbol (|), the rest of the line is treated as the command line for a program (or script) that gets run ๐Ÿš€

the pipe template gets split on spaces into an argument list before the template parameters are expanded ๐Ÿงฉ


๐Ÿ”— SEE ALSO ๐Ÿ”—


๐Ÿ› REPORTING BUGS ๐Ÿ›

  • Security vulnerabilities should be emailed to security@ruby-lang.org. Reported problems get published after they're fixed ๐Ÿ”’
  • Other bugs and feature requests can go to the Ruby Issue Tracking System (https://bugs.ruby-lang.org/). > Do not report security vulnerabilities through this system, because it publishes them immediately ๐Ÿšจ

๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

Ruby was designed and implemented by Yukihiro Matsumoto <matz@netlab.jp> ๐Ÿ’–

see โŸจhttps://github.com/ruby/ruby/graphs/contributorsโŸฉ for everyone else who contributed to Ruby ๐Ÿซถ


UNIX, April 14, 2018, RUBY(1) โ€” that's the whole page, bestie, you're officially a Ruby manpage pro now ๐ŸŽ€โœจ periodt ๐Ÿ’…

๐Ÿ“ฆ where this page comes from