zenspider.com by ryan davis

Ruby QuickRef

Table of Contents


General Tips

These are tips I’ve given over and over and over and over…

See http://github.com/chneukirchen/styleguide/raw/master/RUBY-STYLE for more.

General Syntax Rules

Reserved Words

alias   and     BEGIN   begin   break   case    class   def     defined?
do      else    elsif   END     end     ensure  false   for     if
in      module  next    nil     not     or      redo    rescue  retry
return  self    super   then    true    undef   unless  until   when
while   yield


Basic types are numbers, strings, ranges, regexen, symbols, arrays, and hashes. Also included are files because they are used so often.


0xffff   # hex
0b01011  # binary
0377     # octal
?a       # ASCII character (1.8 only -- 1.9 returns a string "a")
?\C-a    # Control-a
?\M-a    # Meta-a
?\M-\C-a # Meta-Control-a


In all of the %() cases below, you may use any matching characters or any single character for delimiters. %[], %!!, %@@, etc.

'no interpolation'
"#{interpolation}, and backslashes\n"
%q(no interpolation)
%Q(interpolation and backslashes)
%(interpolation and backslashes)
`echo command interpretation with interpolation and backslashes`
%x(echo command interpretation with interpolation and backslashes)
\t (tab), \n (newline), \r (carriage return), \f (form feed), \b
(backspace), \a (bell), \e (escape), \s (whitespace), \nnn (octal),
\xnn (hexadecimal), \cx (control x), \C-x (control x), \M-x (meta x),
\M-\C-x (meta control x)
Here Docs:
<<identifier   - interpolated, goes until identifier
<<"identifier" - same thing
<<'identifier' - no interpolation
<<-identifier  - you can indent the identifier by using "-" in front

Waaaay too much to cover here. Try these instead:


Internalized String. Guaranteed to be unique and quickly comparable. Ideal for hash keys.

1.8: Symbols may not contain \0 or be empty.

:symbol                        == :symbol
:'#{"without"} interpolation'  == :"#{"without"} interpolation"
:"#{"with"} interpolation"     == :"with interpolation"
%s(#{"without"} interpolation) == :"#{"without"} interpolation"


(1..10)  === 5   # true
(1..10)  === 10  # true
(1...10) === 10  # false
(1..10)  === 15  # false

You can define your own by making them Comparable and implementing #succ.

class RangeThingy
  include Comparable

  def <=> rhs
    # ...

  def succ
    # ...

range = RangeThingy.new(lower_bound)..RangeThingy.new(upper_bound)


Test out your regexen in irb or on: http://www.rubyxp.com/ or http://rubular.com.

Usual recommended form:

str =~ /regex/

Lexical options:

/normal regex/iomx[neus]
%r{alternate form} # (where {} can be any character XX or pair () [] etc)


/i         case insensitive
/o         only interpolate #{} blocks once
/m         multiline mode - '.' will match newline
/x         extended mode - whitespace is ignored
/[neus]    encoding: none, EUC, UTF-8, SJIS, respectively

regex characters:

.             any character except newline
[ ]           any single character of set
[^ ]          any single character NOT of set
*             0 or more previous regular expression
*?            0 or more previous regular expression (non-greedy)
+             1 or more previous regular expression
+?            1 or more previous regular expression (non-greedy)
?             0 or 1 previous regular expression
|             alternation
( )           grouping regular expressions
^             beginning of a line or string
$             end of a line or string
{m,n}         at least m but most n previous regular expression
{m,n}?        at least m but most n previous regular expression (non-greedy)
\1-9          nth previous captured group
\&            whole match
\`            pre-match
\'            post-match
\+            highest group matched
\A            beginning of a string
\b            backspace(0x08)(inside[]only)
\b            word boundary(outside[]only)
\B            non-word boundary
\d            digit, same as[0-9]
\D            non-digit
\S            non-whitespace character
\s            whitespace character[ \t\n\r\f]
\W            non-word character
\w            word character[0-9A-Za-z_]
\z            end of a string
\Z            end of a string, or before newline at the end
(?#)          comment
(?:)          grouping without backreferences
(?=)          zero-width positive look-ahead assertion
(?!)          zero-width negative look-ahead assertion
(?>)          nested anchored sub-regexp. stops backtracking.
(?imx-imx)    turns on/off imx options for rest of regexp.
(?imx-imx:re) turns on/off imx options, localized in group.
(?<=)         zero-width positive look-behind assertion.
(?<!)         zero-width negative look-behind assertion.

special character classes:

[:alnum:]   alpha-numeric characters
[:alpha:]   alphabetic characters
[:blank:]   whitespace - does not include tabs, carriage returns, etc
[:cntrl:]   control characters
[:digit:]   decimal digits
[:graph:]   graph characters
[:lower:]   lower case characters
[:print:]   printable characters
[:punct:]   punctuation characters
[:space:]   whitespace, including tabs, carriage returns, etc
[:upper:]   upper case characters
[:xdigit:]  hexadecimal digits


[1, 2, 3]
%w(foo bar baz #{1+1}) == ["foo", "bar", "baz", "\#{1+1}"]
%W(foo bar baz #{1+1}) == ["foo", "bar", "baz", "2"]

Indexes may be negative, and they index backwards (eg -1 is last element).


{1=>2, 2=>4, 3=>6}
{ key: val } == { :key => val } # 1.9 only.


Common methods include:

path = File.join(p1, p2, ... pN) # => "p1/p2/.../pN"

f = File.new("path", "r") # don't use this. Use the block form
File.open("path")      { |f| f.read }
File.open("path", "r") { |f| f.read }
File.open("path", "w") { |f| f.puts "woot" }
File.open("iso-8859-1.txt", "r:iso-8859-1") { |f| ... } # 1.9 open with encoding
File.size("path")  # => 42
File.mtime("path") # => Yesterday

IO.foreach("path") { |line| puts line if line =~ /woot/ }
lines = IO.readlines("path")
Mode Strings
R/O, start of file (default mode).
R/W, start of file.
W/O, truncates or creates.
R/W, truncates or creates.
W/O, end of file or creates.
R/W, end of file or creates.
Binary file mode, in addition to above. DOS/Windows only.



Pseudo variables

self     # the receiver of the current method
nil      # the sole instance of the Class NilClass (falsey)
true     # the sole instance of the Class TrueClass
false    # the sole instance of the Class FalseClass
__FILE__ # the current source file name.
__LINE__ # the current line number in the source file.

Pre-defined variables

Some globals have actual readable names:

$DEBUG     # The boolean status of the -d switch.
$FILENAME  # Current input file from ARGF. Same as ARGF.filename.
$LOAD_PATH # Load path for scripts and binary modules by load or require.
$stderr    # The current standard error output.
$stdin     # The current standard input.
$stdout    # The current standard output.
$VERBOSE   # The verbose flag, which is set by the -v switch.

But most don’t:

$!  # The exception object passed to #raise.
$@  # The stack backtrace generated by the last exception raised.
$&  # Depends on $~. The string matched by the last successful match.
$`  # Depends on $~. The string to the left of the last successful match.
$'  # Depends on $~. The string to the right of the last successful match.
$+  # Depends on $~. The highest group matched by the last successful match.
$1  # Depends on $~. The Nth group of the last successful match. May be > 1.
$~  # The MatchData instance of the last match. Thread and scope local. MAGIC
$=  # The flag for case insensitive. Defaults to nil. Deprecated.
$/  # The input record separator (eg #gets). Defaults to newline.
$\  # The output record separator (eg #print and IO#write). Default is nil.
$,  # The output field separator for the print and Array#join. Defaults to nil.
$;  # The default separator for String#split. See -F flag.
$.  # The current line number of the last file from input.
$<  # See ARGF.
$>  # The default output for print, printf. Defaults to $stdout.
$_  # The last input line of string by gets or readline. Thread and scope local.
$0  # Contains the name of the script being executed. May be assignable.
$*  # See ARGV.
$$  # The process number of the Ruby running this script. Read only.
$?  # The status of the last executed child process. Read only. Thread local.
$:  # See $LOAD_PATH.
$"  # The array contains the module names loaded by require.

Many command line arguments have an associated global, which is usually just an alias to a real global:

$-0  # See $/.
$-a  # Autosplit mode. True if option -a is set. Read-only variable.
$-d  # See $DEBUG.
$-F  # See $;.
$-i  # In in-place-edit mode, this variable holds the extension, otherwise nil.
$-I  # See $LOAD_PATH.
$-l  # True if option -l is set. Read-only.
$-p  # True if option -p is set. Read-only.
$-v  # See $VERBOSE.
$-w  # True if option -w is set.

require “English”

# Small Medium Large

  $!           $ERROR_INFO
  $@           $ERROR_POSITION
  $_           $LAST_READ_LINE
  $>           $DEFAULT_OUTPUT
  $<           $DEFAULT_INPUT
  $$    $PID   $PROCESS_ID
  $?           $CHILD_STATUS
  $~           $LAST_MATCH_INFO
  $=           $IGNORECASE
  $*           $ARGV
  $&           $MATCH
  $`           $PREMATCH
  $'           $POSTMATCH
  $+           $LAST_PAREN_MATCH

Pre-defined global constants

STDIN             # The standard input. The default value for $stdin.
STDOUT            # The standard output. The default value for $stdout.
STDERR            # The standard error output. The default value for $stderr.
ENV               # The hash contains current environment variables. Writable.
ARGF              # A meta-IO across all files in ARGV. (eg ARGF.each_line...)
ARGV              # An array of all the arguments given on run.
DATA              # An IO pointing just after __END__ of the running script.
RUBY_ENGINE       # The ruby implementation you're running (eg ruby, rubinius, etc)
RUBY_PLATFORM     # The platform identifier.
RUBY_VERSION      # The ruby version string (VERSION was deprecated).


Operators and Precedence

(Top to bottom)
:: .
-(unary) +(unary) ! ~
*  /  %
+  -
<<  >>
|  ^
>  >=  <  <=
<=> == === != =~ !~
.. ...
=(+=, -=...)
and or
TODO: add   = and others

All of the above are just methods except these:

=, ::, ., .., ..., !, not, &&, and, ||, or, !=, !~

In addition, assignment operators(+= etc.) are not user-definable.

NOTE: 1.9 has a horrible extension to allow you to define !=, !~, !, and not. A special place in hell is reserved for you if you define any of these.

Control Expressions

if bool-expr [then]
elsif bool-expr [then]

unless bool-expr [then]

expr if     bool-expr
expr unless bool-expr

bool-expr ? true-expr : false-expr

case target-expr
when comparison [, comparison]... [then]
when comparison [, comparison]... [then]
# ...
else # optional else

Case comparisons may be regexen, classes, whatever. Uses #===.

loop do

while bool-expr [do]

until bool-expr [do]

end while bool-expr

end until bool-expr

for name [, name]... in expr [do]

expr.each do | name [, name]... | # preferred form over `for`

expr while bool-expr
expr until bool-expr

break # terminates loop immediately.
redo  # immediately repeats w/o rerunning the condition.
next  # starts the next iteration through the loop.
retry # restarts the loop, rerunning the condition.

Invoking a Method

Nearly everything available in a method invocation is optional, consequently the syntax is very difficult to follow. Here are some examples:

Class::method # don't use this

method(key1 => val1, key2 => val2) # one hash arg, not 2

method(arg1, *[arg2, arg3]) == method(arg1, arg2, arg3)

# As ugly as you want it to be:
method(arg1, key1 => val1, key2 => val2, *splat_arg) #{ block }

The argument syntax is fairly complex:

invocation := [receiver ('::' | '.')] name [ parameters ] [ block ]
parameters := ( [param]* [',' hashlist] ['*' array] [&aProc] )
block      := '{' blockbody '}' | 'do' blockbody 'end'

Defining a Class

Class names begin w/ capital character.

class Identifier [< superclass ]

# singleton classes, add methods to a single instance
class << obj

Defining a Module

module Identifier

Defining a Method

def method_name(arg_list, *list_expr, &block_expr)

# singleton method
def expr.identifier(arg_list, *list_expr, &block_expr)

Access Restriction

totally accessible.
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)
accessible only by instances of class (must be called nekkid no “self.” or anything else).
class A
  # Restriction used w/o arguments set the default access control.

  def protected_method
    # nothing

class B < A
  def test_protected
    myA = A.new

  # Used with arguments, sets the access of the named methods and constants.
  public :test_protected

b = B.new.test_protected


Class Module provides the following utility methods:

attr_reader   :attribute [, :attribute]...  # Creates reader methods
attr_writer   :attribute [, :attribute]...  # Creates setter methods
attr_accessor :attribute [, :attribute]...  # Creates both readers and writers


alias         new   old # symbol syntax not needed... bewilderingly
alias        :new  :old # comma not needed either... go figure
alias_method :new, :old

Creates a new reference to whatever old referred to. old can be any existing method, operator, global. It may not be a local, instance, constant, or class variable.

Blocks, Closures, and Procs


      invocation do ... end
      invocation { ... }

Proc Objects

Created via:

proc      { |args| ... } # {} or do/end
Proc.new  { |args| ... }
lambda    { |args| ... }
-> (args) { ... }        # 1.9+ only
&:method_name            # calls Symbol#to_proc creating: proc { |o| o.method_name }

# or by invoking a method w/ a block argument and catching it on the
# calling side with a &block_arg:

def my_method &block
  block.call 42

obj.my_method { |o| ... }

# in 1.9+, Proc aliases #=== to #call so you can use them as case conditions:

case []
when :empty?.to_proc then
  # ...
when -> (o) { o > 42 && o.prime? } then
  # ...

See class Proc for more information.

Exceptions, Catch, and Throw

Raising and Rescuing

raise ExceptionClass[, "message"]

[rescue [error_type [=> var],...]
  expr...] ...

Catch and Throw

catch :label do
  throw :label if condition?

Standard Library

Ruby comes with an extensive library of classes and modules. Some are built-in, and some are part of the standard library. You can distinguish the two by the fact that the built-in classes are in fact, built-in. There are no dot-rb files for them.

This list is not comprehensive. Use ri Class_and/or_method to look up documentation or try http://ruby-doc.org.

Built-in Library

Class Hierarchy


Standard Library

The essentials:

a simple benchmarking utility
CGI data - simpler than cgi.rb
CGI interaction
date object (compatible)
ruby debugger
delegate messages to other object
access global variables by english names
file utility methods for copying, moving, removing, etc.
traverse directory tree
UTF-8 and Japanese String helpers (replaces String methods)
Networking classes of all kinds
observer design pattern library (provides Observable)
good wrapper for net/http, net/https and net/ftp
open subprocess connection stdin/stdout/stderr
python style object (freeform assignment to instance vars)
argument parser using getopts
prettier debugging output, ‘p’ on steroids.
ruby profiler - find that slow code!
persistent object strage using marshal
singleton design pattern library
lets you use an IO attached to a string.
temporary file that automatically removed
1.9: unit testing framework. (see below)
unit testing framework (1.9: compat library built on top of minitest)
extension to Time class with a lot of converters
execution tracer
Fairly spiffy web server
alternative readable serialization format


Minitest ships with 1.9 by default. You can ensure you have the latest code by installing the latest minitest gem. Minitest will automatically look for the latest minitest gem if you have any installed.

Unit Test Example

require "minitest/autorun"
require "noun"

class TestNoun < Minitest::Test
  def setup
    @noun = Noun.new

  def test_verb
    assert_equal 42, @noun.verb

  # ... more tests ...

Unit Spec Example

require "minitest/autorun"
require "noun"

describe Noun do
  before do
    @noun = Noun.new

  it "verbs the noun" do
    @noun.verb.must_equal 42

  describe "is nestable noun" do
    # ...


Every assertion (except assert_silent) takes an optional message argument at the end. But they also build their own messages on failure, so you really don’t need to provide one except to disambiguate things.

assert truthiness

assert_equal :expected_value, object.result
assert_same expected, object.result
assert_nil object.result
assert_in_delta 42.0, object.number
assert_in_epsilon 42.0, object.number

assert_match(/matcher/, any_obj_not_just_strings) # uses =~
assert_empty collection_or_string
assert_includes object.collection_or_string, :expected_element

assert_instance_of Array, collection
assert_kind_of Enumerable, collection
assert_respond_to object, :method

assert_operator object.result, :truthy?
assert_operator object.result, :<=, 42
assert_predicate object.result, :truthy?
assert_send [recv, msg, arg1, arg2]

assert_output("did something", "") { object.do_something_talky }
assert_silent { object.do_something_quiet }

assert_raises(MyException) { object.do_something_bad }
assert_throws(:my_throw) { object.do_something_throwy }


Not every assertion has a corresponding refutation. Some simply don’t make sense (eg refute_raises – any unexpected exception is automatically an error) or don’t lend any value because they don’t actually validate behavior / side effects (refute_silent – great… it output something… but what?).

refute falsiness

refute_equal :unexpected_value, object.result
refute_same expected, object.result
refute_nil object.result
refute_in_delta 42.0, object.number
refute_in_epsilon 42.0, object.number

refute_match(/matcher/, any_obj_not_just_strings) # still uses =~
refute_empty collection_or_string
refute_includes collection, :unexpected_element

refute_instance_of Array, not_a_collection
refute_kind_of Enumerable, not_a_collection
refute_respond_to object, :method

refute_operator object.result, :falsey?
refute_operator object.result, :<=, 42
refute_predicate object.result, :falsey?


All expectations (positive or otherwise) map to their corresponding assertion/refutation above.

object.result.must_equal 42
object.result.must_be_same_as expected_object
object.number.must_be_close_to 42.0
object.number.must_be_within_epsilon 42.0

object.collection.must_include :expected_element
object.any_obj_not_just_strings.must_match matcher

object.result.must_be_instance_of Array
object.collection.must_be_kind_of Enumerable
object.must_respond_to :message

object.result.must_be :<=, 42
object.collection_or_string.must_be :empty?

proc { object.do_something_bad }.must_raise exception
proc { object.do_something_throwy }.must_throw :my_throw

proc { object.do_something_talky }.must_output "something"
proc { object.do_something_quiet }.must_be_silent

Negative Expectations

object.result.wont_equal 42
object.result.wont_be_same_as unexpected_object
object.number.wont_be_close_to 42.0
object.number.wont_be_within_epsilon 42.0

collection.wont_include :unexpected_element
object.any_obj_not_just_strings.wont_match matcher

object.result.wont_be_instance_of Array
object.not_a_collection.wont_be_kind_of Enumerable

object.result.wont_be :<=, 42
object.collection_or_string.wont_be :empty?
object.wont_respond_to :message

Helper methods

Usable in tests or specs.

out, err = capture_io { object.do_something_talky }
out, err = capture_subprocess_io { `cmd arg` }
flunk "This totally fails"
pass "OCD people need assertion counts to rise"



Command Line Options

-0[octal]       specify record separator (\0, if no argument).
-a              autosplit mode with -n or -p (splits $_ into $F).
-c              check syntax only.
-Cdirectory     cd to directory, before executing your script.
--copyright     print the copyright and exit.
-d              set debugging flags (set $DEBUG to true).
-e 'command'    one line of script. Several -e's allowed.
-F regexp       split() pattern for autosplit (-a).
-h		  prints summary of the options.
-i[extension]   edit ARGV files in place (make backup if extension supplied).
-Idirectory     specify $LOAD_PATH directory (may be used more than once).
-Kkcode         specifies KANJI (Japanese) code-set.
-l              enable line ending processing.
-n              assume 'while gets(); ... end' loop around your script.
-p              assume loop like -n but print line also like sed.
-rlibrary       require the library, before executing your script.
-s              enable some switch parsing for switches after script name.
-S              look for the script using PATH environment variable.
-T[level]       turn on tainting checks.
-v              print version number, then turn on verbose mode.
--version       print the version and exit.
-w              turn warnings on for your script.
-x[directory]   strip off text before #! line and perhaps cd to directory.
-X directory    causes Ruby to switch to the directory.
-y              turns on compiler debug mode.

Environment Variables

DLN_LIBRARY_PATH Search path for dynamically loaded modules.
RUBYLIB          Additional search paths.
RUBYLIB_PREFIX   Add this prefix to each item in RUBYLIB. Windows only.
RUBYOPT          Additional command line options.
RUBYPATH         With -S, searches PATH, or this value for ruby programs.
RUBYSHELL        Shell to use when spawning. (Windows (and OS/2!) only)


irb [options] [script [args]]

The essential options are:

-d              Sets $DEBUG to true. Same as "ruby -d ..."
-f              Prevents the loading of ~/.irb.rc.
-h              Get a full list of options.
-m              Math mode. Overrides --inspect. Loads "mathn.rb".
-r module       Loads a module. Same as "ruby -r module ..."
-v              Prints the version and exits.
--inf-ruby-mode Turns on emacs support and turns off readline.
--inspect       Turns on inspect mode. Default.
--noinspect     Turns off inspect mode.
--noprompt      Turns off the prompt.
--noreadline    Turns off readline support.
--prompt        Sets to one of 'default', 'xmp', 'simple', or 'inf-ruby'.
--readline      Turns on readline support. Default.
--tracer        Turns on trace mode.

Besides arbitrary ruby commands, the special commands are:

exit                  exits the current session, or the program
fork block            forks and runs the given block
cb args               changes to a secified binding
source file           loads a ruby file into the session
irb [obj]             starts a new session, with obj as self, if specified
conf[.key[= val]]     access the configuration of the session
jobs                  lists the known sessions
fg session            switches to the specifed session
kill session          kills a specified session

Session may be specified via session#, thread-id, obj, or self.


To invoke the debugger:

ruby -r debug ...

To use the debugger:

b[reak] [file:|class:]<line|method
b[reak] [class.]<line|method
                           set breakpoint to some position
wat[ch] expression         set watchpoint to some expression
cat[ch] exception          set catchpoint to an exception
b[reak]                    list breakpoints
cat[ch]                    show catchpoint
del[ete][ nnn]             delete some or all breakpoints
disp[lay] expression       add expression into display expression list
undisp[lay][ nnn]          delete one particular or all display expressions
c[ont]                     run until program ends or hit breakpoint
s[tep][ nnn]               step (into methods) one line or till line nnn
n[ext][ nnn]               go over one line or till line nnn
w[here]                    display frames
f[rame]                    alias for where
l[ist][ (-|nn-mm)]         list program, - lists backwards
                           nn-mm lists given lines
up[ nn]                    move to higher frame
down[ nn]                  move to lower frame
fin[ish]                   return to outer frame
tr[ace] (on|off)           set trace mode of current thread
tr[ace] (on|off) all       set trace mode of all threads
q[uit]                     exit from debugger
v[ar] g[lobal]             show global variables
v[ar] l[ocal]              show local variables
v[ar] i[nstance] object    show instance variables of object
v[ar] c[onst] object       show constants of object
m[ethod] i[nstance] obj    show methods of object
m[ethod] class|module      show instance methods of class or module
th[read] l[ist]            list all threads
th[read] c[ur[rent]]       show current thread
th[read] [sw[itch]] nnn    switch thread context to nnn
th[read] stop nnn          stop thread nnn
th[read] resume nnn        resume thread nnn
p expression               evaluate expression and print its value
h[elp]                     print this help
everything else            evaluate
empty                      repeats the last command

Original URL: www.zenspider.com/Languages/Ruby/QuickRef.html
$Author: ryand $
$Date: 2015/04/21 $
$Revision: #10 $