RUBY(1)

πŸ“¦ version 3.0.2, in ubuntu jammy

πŸ’– RUBY(1) β€” Ruby Programmer's Reference Guide πŸ’–

okay bestie, sit down, hydrate, and let me tell you about ruby β€” this is the manual page for the Ruby interpreter, from the Ruby Programmer's Reference Guide, and yes it's a whole vibe. πŸ¦‹


✨ 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] [--]
     [program_file] [argument ...]

yes that's a lot of flags, and yes, we're gonna get to every single one of them eventually. not today, but eventually. bestie patience. 🫢


πŸ’œ DESCRIPTION πŸ’œ

Ruby is an <strong>interpreted scripting language for quick and easy object-oriented programming</strong>.</purgle>

Let me fix that β€” Ruby is an interpreted scripting language for quick and easy object-oriented programming. ✨ It's got a whole arsenal of features for processing text files and for doing system management tasks (like in Perl, iykyk). It is simple, straightforward, and extensible. periodt. πŸ’…

So like β€” if you want a language for easy object-oriented programming, or you're just not feeling the Perl ugliness 😭, or you DO love the concept of LISP but the parentheses are giving you a migraine β€” Ruby might be your language of choice. πŸŽ€


🌈 FEATURES 🌈

Ruby's features are as follows, and honestly? the way this list just keeps going. main character energy fr fr. πŸ‘‘

🧚 Interpretive

Ruby is an interpreted language, so you don't have to recompile programs written in Ruby to execute them. no build step bestie, we just run it. slay. ✨

πŸ¦‹ Variables have no type (dynamic typing)

Variables in Ruby can contain data of any type. You don't have to worry about variable typing. Consequently, it has a weaker compile time check. πŸ’€

🌸 No declaration needed

You can use variables in your Ruby programs without any declarations. Variable names denote their scope β€” global, class, instance, or local. πŸŽ€

πŸ’… Simple syntax

Ruby has a simple syntax influenced slightly from Eiffel. cute, cultured, well-read. πŸ’–

πŸ“ No user-level memory management

Ruby has automatic memory management. Objects no longer referenced from anywhere are automatically collected by the garbage collector built into the interpreter. you will not be calling free() bestie, she's got it handled. 🫢

⭐ Everything is an object

Ruby is a purely object-oriented language, and was so <strong>since its creation</strong>. Even such basic data as integers are seen as objects. yes even the humble integer. iconic behavior. πŸ‘‘

🌈 Class, inheritance, and methods

Being an object-oriented language, Ruby naturally has basic features like classes, inheritance, and methods. the classics. πŸŽ€

🧚 Singleton methods

Ruby has the ability to define methods for certain objects. For example, you can define a press-button action for certain widget by defining a singleton method for the button. Or β€” if you're feeling spicy β€” you can make up your own prototype based object system using singleton methods. go off. πŸ’…

πŸ’œ Mix-in by modules

Ruby intentionally does not have the multiple inheritance, because it is a source of confusion. we don't do chaos here (affectionate). Instead, Ruby has the ability to 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 bunch of text processing features like in Perl. πŸ’–

🌸 M17N, character set independent

Ruby supports multilingualized programming. Easy to process texts written in many different natural languages and encoded in many different character encodings, <strong>without dependence on Unicode</strong>. the way that it just does this. obsessed. 🌈

πŸ‘‘ Bignums

With built-in bignums, you can for example calculate <code>factorial(400)</code>. go ahead, try it, she's not scared. πŸ”₯

πŸ’… Reflection and domain specific languages

<strong>Class is also an instance of the <code>Class</code> class.</strong> Definition of classes and methods is an expression just as <code>1+1</code> is. So your programs can even write and modify programs. Thus you can write your application in your own programming language on top of Ruby. it's giving meta. it's giving inception. it's giving slay. πŸ’€

🧚 Exception handling

As in Java(tm). 😭

πŸ’œ Direct access to the OS

Ruby can use most Unix system calls, 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

In addition to the β€œbuiltin libraries” and β€œstandard libraries” that are bundled with Ruby, a vast amount of third-party libraries (β€œgems”) are available via the package management system called β€˜RubyGems’, namely the <code>gem(1)</code> command. 🫢

Visit RubyGems.org (<code>https://rubygems.org/</code>) to find the gems you need, and explore GitHub (<code>https://github.com/</code>) to see how they are being developed and used. πŸ’–


okay that's part 1 bestie, the flags are coming and there are SO many of them, stay with me 🌸

✨ OPTIONS ✨

okay bestie, so the Ruby interpreter takes all these command-line options (we call 'em switches), and ngl they're pretty similar to perl(1)'s. Here's the whole menu, no skips πŸ’–

  • --copyright β€” prints the copyright notice and dips immediately, without running any script. iconic legal moment ✨
  • --version β€” prints the version of the Ruby interpreter and quits right away, no script running. just vibes and a number πŸ’…
  • -0[octal] β€” (that's the digit β€œzero”, bestie, don't get it twisted πŸ‘€) sets the input record separator ($/) to an octal number. If you don't give it a digit, the null character becomes the separator. Other switches can follow the digits, no problem. -00 turns Ruby into paragraph mode 🌸. And -0777 makes Ruby read the whole file at once as one single string, because there's literally no legal character with that value. slay.
  • -C directory, -X directory β€” makes Ruby switch to that directory. like a lil cd but for Ruby's soul πŸ¦‹
  • -E external[:internal], --encoding external[:internal] β€” sets the default value(s) for external encodings and internal encoding. Separate the values with a colon (:).

You can totally leave out the internal one β€” then the value (Encoding.default_internal) will be nil. no notes πŸ’œ

  • --external-encoding=encoding, --internal-encoding=encoding β€” specify the default external or internal character encoding. pick your fighter πŸŽ€
  • -F pattern β€” sets the input field separator ($;).
  • -I directory β€” tells Ruby where to load the library scripts from. The directory path gets added to the load-path variable ($:). path = loaded πŸ’…
  • -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:
    • e β€” EUC-JP
    • s β€” Windows-31J (CP932)
    • u β€” UTF-8
    • n β€” ASCII-8BIT (BINARY)
  • -S β€” makes Ruby use the PATH environment variable to hunt down the script, unless its name starts with a slash. This is how you emulate #! on machines that don't support it, like this:
#! /usr/local/bin/ruby
# This line makes the next one a comment in Ruby \
  exec /usr/local/bin/ruby -S $0 $*

On some systems $0 doesn't always hold the full pathname, so you need the -S switch to tell Ruby to search for the script when necessary (to handle embedded spaces and all that jazz). A better construct than $* would be ${1+"$@"}, but it doesn't work if the script is being interpreted by csh(1). RIP 🫢

  • -T[level=1] β€” turns on taint checks at the specified level (default is 1). safety first, bestie πŸ”₯
  • -U β€” sets the default value for internal encodings (Encoding.default_internal) to UTF-8. as it should be πŸ’…
  • -W[level=2] β€” turns on verbose mode at the specified level without printing the version message at the start. The level can be:
    • 0 β€” Verbose mode is "silence". It sets $VERBOSE to nil.
    • 1 β€” Verbose mode is "medium". It sets $VERBOSE to false.
    • 2 (default) β€” Verbose mode is "verbose". It sets $VERBOSE to true. -W2 is the same as -w.
  • -a β€” turns on auto-split mode when you use it with -n or -p. In auto-split mode, Ruby runs
$F = $_.split

at the beginning of each loop. automatic, no cap ✨

  • -c β€” makes Ruby check the syntax of the script and exit without executing it. If there are no syntax errors, Ruby prints β€œSyntax OK” to standard output. we love a syntax girly πŸŽ€
  • -d, --debug β€” turns on debug mode. $DEBUG gets set to true. πŸ”
  • -e command β€” specifies the script straight from the command line, while telling Ruby not to search the rest of the arguments for a script file name. very main-character energy πŸ’œ
  • -h, --help β€” prints a summary of the options. the classic "help me" bestie πŸ™ˆ
  • -i extension β€” sets in-place-edit mode. The extension, if you give one, gets added to the old file name to make a backup copy. For 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

backup secured, original preserved, both files living their truth 🌈

  • -l β€” (that's the lowercase letter β€œell”, bestie 😭) enables automatic line-ending processing, which means: first it sets $\ to the value of $/, and second it chops every line read using chomp!.
  • -n β€” makes Ruby assume this loop wraps around your script, so it iterates over the file name arguments kinda like sed -n or awk:
while gets
  ...
end
  • -p β€” acts mostly the same as -n, except it prints the value of the variable $_ at the end of each loop. For example:
% echo matz | ruby -p -e '$_.tr! "a-z", "A-Z"'
MATZ

obsessed with how clean that is πŸ’…

  • -r library β€” makes Ruby load the library using require. Super useful when you're using -n or -p. πŸ“
  • -s β€” enables some switch parsing for switches that come after the script name but before any file name arguments (or before a --). Any switches it finds there get removed from ARGV and set as the corresponding variable in the script. For example:
#! /usr/local/bin/ruby -s
# prints "true" if invoked with `-xyz' switch.
print "true\n" if $xyz
  • -v β€” enables verbose mode. Ruby prints its version at the start and sets the variable $VERBOSE to true. Some methods print extra messages when this variable is true. And heads up: if you give this switch and no other switches are present, Ruby quits right after printing its version. dramatic exit, respect ✨
  • -w β€” enables verbose mode without printing the version message at the beginning. It sets the $VERBOSE variable to true.
  • -x[directory] β€” tells Ruby the script is embedded in a message. Leading garbage gets discarded until the first line that starts with β€œ#!” and contains the string β€œruby”. Any meaningful switches on that line get applied. The end of the script must be specified with either EOF, ^D (control-D), ^Z (control-Z), or the reserved word __END__. If you specify a directory name, Ruby switches to that directory before executing the script. 🧚
  • -y, --yydebug β€” DO NOT USE. 🚨

Turns on compiler debug mode. Ruby will print a whole bunch of internal state messages during compilation. Only specify this switch if you're actually going to debug the Ruby interpreter. like, fr fr, don't just casually use this one bestie πŸ’€

  • --disable-FEATURE, --enable-FEATURE β€” disables (or enables) the specified FEATURE. Here's the roster:
    • --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, false if otherwise. πŸ’Ž
    • --disable-rubyopt, --enable-rubyopt β€” ignores (or considers) the RUBYOPT environment variable. By default, Ruby considers the variable.
    • --disable-all, --enable-all β€” disables (or enables) all features. all or nothing, periodt πŸ‘‘
  • --dump=target β€” dumps some information.

Prints the specified target. target can be one of:

  • version β€” version description, same as --version
  • usage β€” brief usage message, same as -h
  • help β€” Show long help message, same as --help
  • syntax β€” check of syntax, same as -c --yydebug
  • yydebug β€” compiler debug mode, same as --yydebug
Only specify this switch if you are going to debug the Ruby interpreter. 🚨
  • parsetree
  • parsetree_with_comment β€” AST nodes tree
Only specify this switch if you are going to debug the Ruby interpreter. 🚨
  • insns β€” disassembled instructions
Only specify this switch if you are going to debug the Ruby interpreter. 🚨
  • --verbose β€” enables verbose mode without printing the version message at the beginning. It sets the $VERBOSE variable to true. And heads up: if you give this switch and there are no script arguments (no script file or -e options), Ruby quits immediately. πŸ¦‹

🌸 ENVIRONMENT 🌸

  • RUBYLIB β€” a colon-separated list of directories that get added to Ruby's library load path ($:). Directories from this environment variable are searched before the standard load path gets searched.

e.g.:

RUBYLIB="$HOME/lib/ruby:$HOME/lib/rubyext"
  • RUBYOPT β€” additional Ruby options. ✨

e.g.

RUBYOPT="-w -Ke"

Note that RUBYOPT can contain only -d, -E, -I, -K, -r, -T, -U, -v, -w, -W, --debug, --disable-FEATURE and --enable-FEATURE. that's the whole guest list, don't try to sneak anyone else in πŸ’…

  • RUBYPATH β€” a colon-separated list of directories that Ruby searches for Ruby programs when the -S flag is specified. This variable comes before the PATH environment variable. πŸ‘‘
  • RUBYSHELL β€” the path to the system shell command. This environment variable is enabled for only mswin32, mingw32, and OS/2 platforms. If this variable isn't defined, Ruby refers to COMSPEC. 🐚
  • PATH β€” Ruby refers to the PATH environment variable when calling Kernel#system.

And Ruby depends on some RubyGems-related environment variables unless RubyGems is disabled. See the help of gem(1) as below. πŸ’Ž

% gem help

πŸ—‘οΈ GC ENVIRONMENT πŸ—‘οΈ

okay bestie, gather round, we're talking about the garbage collector 🫢 The Ruby GC tracks objects in fixed-sized slots, but each object can also have auxiliary memory allocations handled by the malloc family of C standard library calls (malloc(3), calloc(3), and realloc(3)). In this documentation, the "heap" means the Ruby object heap of fixed-sized slots, while "malloc" means auxiliary allocations, which people usually call the "process heap". So basically there are at least two possible ways to trigger GC:

  1. Reaching the object limit. πŸ’€
  2. Reaching the malloc limit. πŸ’€

In Ruby 2.1, the generational GC showed up (iconic behaviour tbh) and the limits got split into young and old generations, which gives us 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 by the following 11 environment variables:

  • RUBY_GC_HEAP_INIT_SLOTS β€” Initial allocation slots. Introduced in Ruby 2.1, default: 10000.
  • RUBY_GC_HEAP_FREE_SLOTS β€” Prepare at least this amount of slots after GC. Allocate this number of slots if there are not enough slots. Introduced in Ruby 2.1, default: 4096.
  • RUBY_GC_HEAP_GROWTH_FACTOR β€” Increase the allocation rate of heap slots by this factor. Introduced in Ruby 2.1, default: 1.8, minimum: 1.0 (no growth).
  • RUBY_GC_HEAP_GROWTH_MAX_SLOTS β€” The allocation rate is limited to this number of slots, which prevents excessive allocation caused by RUBY_GC_HEAP_GROWTH_FACTOR. Introduced in Ruby 2.1, default: 0 (no limit).
  • RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR β€” Perform 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 initial limit of young generation allocation from the malloc-family. GC will start when this limit is reached. Default: 16MB.
  • RUBY_GC_MALLOC_LIMIT_MAX β€” The maximum limit of young generation allocation from malloc before GC starts. Prevents excessive malloc growth caused by RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR. Introduced in Ruby 2.1, default: 32MB.
  • RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR β€” Increases the limit of young generation malloc calls, which reduces GC frequency but increases malloc growth 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 initial limit of old generation allocation from malloc; a full GC will start when this limit is reached. Introduced in Ruby 2.1, default: 16MB.
  • RUBY_GC_OLDMALLOC_LIMIT_MAX β€” The maximum limit of old generation allocation from malloc before a full GC starts. 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 β€” Increases the limit of old generation malloc allocation, which reduces full GC frequency but increases malloc growth until RUBY_GC_OLDMALLOC_LIMIT_MAX is reached. Introduced in Ruby 2.1, default: 1.2, minimum: 1.0 (no growth).

🧡 STACK SIZE ENVIRONMENT 🧡

ngl bestie, these ones are a whole vibe check you have to be careful with. Stack size environment variables are implementation-dependent and subject to change with different versions of Ruby. The VM stack is used for pure-Ruby code and is managed by the virtual machine. The machine stack is used by the operating system, and its usage depends on C extensions as well as C compiler options. Using lower values for these may allow applications to keep more Fibers or Threads running β€” but it increases the chance of SystemStackError exceptions and segmentation faults (SIGSEGV) 😭 These environment variables have been available since Ruby 2.0.0. All values are specified in bytes.

  • RUBY_THREAD_VM_STACK_SIZE β€” VM stack size used at thread creation. default: 131072 (32-bit CPU) or 262144 (64-bit).
  • RUBY_THREAD_MACHINE_STACK_SIZE β€” Machine stack size used at thread creation. default: 524288 or 1048575.
  • RUBY_FIBER_VM_STACK_SIZE β€” VM stack size used at fiber creation. default: 65536 or 131072.
  • RUBY_FIBER_MACHINE_STACK_SIZE β€” Machine stack size used at fiber creation. default: 262144 or 524288.

🌈 SEE ALSO 🌈


πŸ› REPORTING BUGS πŸ›

  • Security vulnerabilities should be reported via an email to security@ruby-lang.org. Reported problems will be published after being fixed.
  • Other bugs and feature requests can be reported via the Ruby Issue Tracking System (https://bugs.ruby-lang.org/).
ok but do NOT report security vulnerabilities via this system, because it publishes the vulnerabilities immediately. no cap. πŸ™ˆ

πŸ‘‘ AUTHORS πŸ‘‘

Ruby is designed and implemented by Yukihiro Matsumoto <matz@netlab.jp>.

See ⟨https://bugs.ruby-lang.org/projects/ruby/wiki/Contributors⟩ for contributors to Ruby. πŸ’–


and that's the whole page bestie, you ate that πŸŽ€ this one's from the UNIX manual, dated April 14, 2018, and it lives at RUBY(1) β€” go forth and slay your Ruby βœ¨πŸ¦‹πŸ’…

πŸ“¦ where this page comes from