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+1is ๐ 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.-00flips Ruby into paragraph mode ๐ and-0777makes 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 thenEncoding.default_internalends up asnil๐ซ--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.kcodecan be one of these:eโ EUC-JPsโ Windows-31J (CP932)uโ UTF-8nโ 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$0doesn't always hold the full pathname, so you need-Sso 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$VERBOSEtonil๐คซ1โ "medium" mode, which sets$VERBOSEtofalse๐2(default) โ "verbose" mode, which sets$VERBOSEtotrue๐ฃ and-W2is the exact same thing as-w
-aโ turns on auto-split mode, but only when you pair it with-nor-pโ๏ธ in auto-split mode, Ruby runs$F = $_.splitat the start of each loop--backtrace-limit=numโ caps how many lines a backtrace can have atnumlines (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$DEBUGgets 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 withchomp!โ๏ธ-nโ makes Ruby wrap your script in a loop that goes over the file name arguments, kinda likesed -nor 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 usingrequire๐ super handy alongside-nor-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 fromARGVand 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$VERBOSEto true. some methods print extra messages when that variable is true. if-vis 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$VERBOSEto 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 withEOF,^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. theGemconstant istrueif RubyGems is enabled andfalseif it isn't--disable-rubyopt,--enable-rubyoptโ ignores (or considers) theRUBYOPTenvironment 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, andtargetcan 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$VERBOSEto true ๐ข if you give this and no script arguments (a script file or-eoptions), Ruby quits immediately ๐โโ๏ธ--crash-report=templateโ sets the template for the path name where the crash report gets saved ๐ฅ check theRUBY_CRASH_REPORTenvironment 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-Sflag. This one goes beforePATHin 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 toCOMSPECPATH๐ โ Ruby checks thePATHenvironment variable when it callsKernel#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:
- Reaching the object limit.
- 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 ๐ธ
- Reaching the old object limit.
- 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 ofGC.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, soRUBY_GC_HEAP_GROWTH_FACTORcan'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 ofRUBY_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, untilRUBY_GC_MALLOC_LIMIT_MAXis 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 byRUBY_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, untilRUBY_GC_OLDMALLOC_LIMIT_MAXis 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) or1048575(64-bit)RUBY_THREAD_MACHINE_STACK_SIZE๐ชก โ machine stack size used when a thread is created. default:524288or1048575RUBY_FIBER_VM_STACK_SIZE๐ฆ โ VM stack size used when a fiber is created. default:65536or131072RUBY_FIBER_MACHINE_STACK_SIZE๐ โ machine stack size used when a fiber is created. default:262144or524288
๐ฅ 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 ๐
https://www.ruby-lang.org/โ the official web site ๐https://www.ruby-toolbox.com/โ comprehensive catalog of Ruby libraries ๐
๐ 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
- debian trixie: ruby3.3 3.3.8-2 ยท BSD-2-Clause OR Ruby
- ubuntu questing: ruby3.3 3.3.8-2ubuntu2
- ubuntu questing: ruby3.3 3.3.8-2ubuntu2.1 ยท BSD-2-Clause OR Ruby
- ubuntu resolute: ruby3.3 3.3.8-2ubuntu3 ยท BSD-2-Clause OR Ruby
- ubuntu resolute: ruby3.3 3.3.8-2ubuntu3.1 ยท BSD-2-Clause OR Ruby
- ubuntu stonking: ruby3.3 3.3.8-2.2ubuntu4 ยท BSD-2-Clause OR Ruby
- ๐ girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms