RUBY(1)

πŸ“¦ version 3.1.2, in debian bookworm

πŸ’Ž RUBY β€” like, the whole vibe check

okay bestie, welcome to the RUBY(1) page β€” Ruby Programmer's Reference Guide, the one and only, the blueprint, the origin story πŸŽ€


✨ NAME ✨

ruby β€” an interpreted, object-oriented scripting language πŸ’… (yes that's the whole name section, she's lowkey humble like that)


🌸 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 ...]

look at all those flags. she contains multitudes. don't worry, we're gonna go through literally every single one, no skipping, I promise 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

Ruby is an interpreted scripting language built for quick and easy object-oriented programming. She's got so many features for processing text files and doing system management tasks (like in Perl, bestie, if you know you know). She's simple, she's straightforward, and she's extensible. Periodt.

So here's the vibe check: if you want a language for easy object-oriented programming, OR you're not into that Perl ugliness, OR you actually love the concept of LISP but cannot deal with that many parentheses? Ruby might be your language of choice. No cap. πŸ’–


πŸ”₯ FEATURES πŸ”₯

Ruby's features, bestie, are as follows:

πŸ’« Interpretive

Ruby is an interpreted language, so you literally don't have to recompile programs written in Ruby to execute them. Write it, run it, done. Main character energy. ✨

πŸ’« Variables have no type (dynamic typing)

Variables in Ruby can hold data of any type. You don't have to worry about variable typing, which is so freeing. Consequently though β€” and this is the tradeoff, bestie β€” it has a weaker compile time check. πŸ™ˆ

πŸ’« No declaration needed

You can use variables in your Ruby programs without any declarations whatsoever. The variable names themselves denote their scope β€” global, class, instance, or local. It's giving effortless. πŸ’…

πŸ’« Simple syntax

Ruby has a simple syntax, influenced slightly from Eiffel. (Eiffel! She's got taste. πŸ‘‘)

πŸ’« No user-level memory management

Ruby has automatic memory management, and honestly? Slay. Objects that are no longer referenced from anywhere get automatically collected by the garbage collector built right into the interpreter. You never think about it, she handles it. 🧚

πŸ’« Everything is an object

Ruby is a purely object-oriented language, and she has been since her creation. Even basic data like integers are seen as objects. Everything. The way she doesn't make exceptions for anybody?? Iconic. ⭐

πŸ’« Class, inheritance, and methods

Being an object-oriented language, Ruby naturally has the basic features β€” classes, inheritance, and methods. The fundamentals, and she ate. πŸ“

πŸ’« Singleton methods

Ruby has the ability to define methods for certain objects. For example, you can define a press-button action for a certain widget by defining a singleton method for the button. Or β€” and this is the fun part β€” you can make up your own prototype based object system using singleton methods, if you want to. Go off, honestly. 🌈

πŸ’« Mix-in by modules

Ruby intentionally does not have multiple inheritance, because that's a source of confusion and she's above the drama. Instead, Ruby can share implementations across the inheritance tree. This is often called a β€˜Mix-in’. πŸ’–

πŸ’« Iterators

Ruby has iterators for loop abstraction. Clean. ✨

πŸ’« Closures

In Ruby, you can objectify the procedure. Yes bestie, the procedure. An object now. πŸ’œ

πŸ’« Text processing and regular expressions

Ruby has a whole 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 β€” and all of that without dependence on Unicode. She's worldly. She contains multitudes. πŸ¦‹

πŸ’« Bignums

With built-in bignums, you can for example calculate factorial(400). Like. Bestie. factorial(400). 😭πŸ”₯

πŸ’« Reflection and domain specific languages

Class is also an instance of the Class class. Definition of classes and methods is an expression just as 1+1 is. So your programs can even write and modify programs. Which means you can write your application in your own programming language on top of Ruby. The power?? Unreal. πŸ‘‘

πŸ’« Exception handling

As in Java(tm). That's it, that's the bullet. πŸ’…

πŸ’« Direct access to the OS

Ruby can use most Unix system calls, which are often used in system programming. She speaks OS. Fluently. πŸ–₯️✨

πŸ’« Dynamic loading

On most Unix systems, you can load object files into the Ruby interpreter on-the-fly. No restart, no ceremony. πŸ’«

πŸ’« 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 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 are being developed and used. πŸŽ€πŸ’Ž

okay that's the features section done, and there's so much more coming β€” don't go anywhere bestie, we're just getting started 🫢

✨ OPTIONS ✨

okay bestie, so the Ruby interpreter takes all these command-line options (switches) and ngl they're very similar to the ones perl(1) has. iconic family resemblance fr fr πŸ’…

  • --copyright β€” prints the copyright notice and dips immediately, no script runs at all. like it said what it said and left 🌸
  • --version β€” prints the version of the Ruby interpreter and bounces right away, again without running any script. short and sweet periodt ✨
  • -0[octal] β€” (that's the digit "zero", bestie, not the letter) 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. -00 turns Ruby into paragraph mode. And -0777 makes Ruby read the whole file at once as a single string, because there is literally no legal character with that value πŸ’€
  • -C directory, -X directory β€” makes Ruby switch to that directory. simple as πŸ¦‹
  • -E external[:internal], --encoding external[:internal] β€” sets the default value(s) for external encodings and internal encoding. The values get separated with a colon (:).

You can totally omit the internal one, and then that value (Encoding.default_internal) will be nil. no drama 🌈

  • --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 ($:). πŸ’–
  • -K kcode β€” sets the KANJI (Japanese) encoding. The default for script encodings (__ENCODING__) and external encodings (Encoding.default_external) becomes the one you picked. 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 search for 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 have the full pathname, so you need the -S switch to tell Ruby to go hunt for the script if needed (to handle embedded spaces and such). A better construct than $* would be ${1+"$@"}, but it does not work if the script is being interpreted by csh(1). rip 😭

  • -T[level=1] β€” turns on taint checks at the level you specify (default is 1). πŸ”₯
  • -U β€” sets the default value for internal encodings (Encoding.default_internal) to UTF-8. slay ✨
  • -W[level=2] β€” turns on verbose mode at the level you specify, without printing the version message at the beginning. 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. 🌸

  • -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. no cap, that's a whole vibe check for your code πŸŽ€
  • -d, --debug β€” turns on debug mode. $DEBUG gets set to true. πŸ›
  • -e command β€” specifies the script from the command line, while telling Ruby not to search the rest of the arguments for a script file name. πŸ¦‹
  • -h, --help β€” prints a summary of the options. your lil cheat sheet bestie πŸ’…
  • -i extension β€” specifies 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 bestie behavior, we love to see it πŸ’–

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

periodt πŸ’…

  • -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 found there get removed from ARGV and set 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 will print its version at the beginning and set the variable $VERBOSE to true. Some methods print extra messages if this variable is true. And if you give this switch and no other switches are present, Ruby quits after printing its version. it just wanted you to know ✨
  • -w β€” enables verbose mode without printing the version message at the beginning. It sets the $VERBOSE variable to true. πŸ”₯
  • -x[directory] β€” tells Ruby that 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 has to be marked with either EOF, ^D (control-D), ^Z (control-Z), or the reserved word __END__. And if you specify the directory name, Ruby switches to that directory before executing the script. 🧚
  • -y, --yydebug β€” DO NOT USE.
bestie I am so serious, do not touch this one πŸ™ˆ

It turns on compiler debug mode. Ruby will print a whole bunch of internal state messages during compilation. Only specify this switch if you are literally going to debug the Ruby interpreter itself. πŸ’€

  • --disable-FEATURE, --enable-FEATURE β€” disables (or enables) the specified FEATURE.
    • --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 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 of them. the whole menu. 🌈
  • --dump=target β€” dump 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 if you give this switch and no script arguments (script file or -e options) are present, Ruby quits immediately. bye bestie πŸ‘‹

🌿 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 is 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 so bestie, the Ruby garbage collector (GC) tracks objects in fixed-sized slots, but each object may 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" refers to the Ruby object heap of fixed-sized slots, while "malloc" refers to auxiliary allocations commonly referred to as the "process heap". Thus 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 was introduced and the limits are divided into young and old generations, providing two additional 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 may 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 slots if there are not enough slots. Introduced in Ruby 2.1, default: 4096
  • RUBY_GC_HEAP_GROWTH_FACTOR β€” Increase 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 β€” Allocation rate is limited to this number of slots, preventing excessive allocation due to 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 due to 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, reducing GC frequency but increasing 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 due to 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, reducing full GC frequency but increasing 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 🧡

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 managed by the virtual machine. Machine stack is used by the operating system and its usage is dependent 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 increases the chance of SystemStackError exceptions and segmentation faults (SIGSEGV). Be careful, bestie β€” that's giving risky. These environment variables are 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: 524288 (32-bit CPU) or 1048575 (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/). Do not report security vulnerabilities via this system because it publishes the vulnerabilities immediately.

πŸ‘‘ AUTHORS πŸ‘‘


πŸ’– That's the Ruby(1) manual wrapped, bestie! Source: UNIX manual, date: April 14, 2018, version: RUBY(1). Slay. ✨

πŸ“¦ where this page comes from