Ruby Quick Ref

Table of Contents

Literal Values

nil Null object
true Boolean true object
false Boolean false object
42 decimal Integer literal
0x41 hexadecimal Integer literal
0777 octal Integer literal
0b1000 binary Integer literal
:foo Symbol literal
'foo' plain String literal
"foo\n" String literal with escape characters
"foo #{var}" String interpolation
%{foo "bar"} String literal with quote characters
/[a-z][a-z0-9]*/ Regular expression literal
[1, 2, 'a', 'b'] Array literal
%w[foo bar baz] same as [“foo”, “bar”, “baz”]
1..100 Range literal
{'a' => 1, 'b' => 2} Hash literal
{a: 1, b: 2} Symbol Hash literal

Variables

var = 42 assign a local variable
var reads a local variable
@var = 42 assign an instance variable
@var reads an instance variable

Operators

Boolean

var is true or another Object besides false or nil
!var is nil or false

Comparison

var1 == var2 equal to
var1 != var2 not equal
var1 > var2 greater than
var1 >= var2 greater than or equal to
var1 < var2 less than
var1 <= var2 less than or equal to

Arithmetic

int + 1 addition
int - 1 subtraction
int * 2 multiplication
int / 2 division
int % 2 modulus
int ** 2 power

Bitwise

int & 0xff bitwise AND
int | 0xff bitwise OR
int ^ 0xff bitwise XOR
int << 8 bitwise logical shift left (also often an alias for append)
int >> 8 bitwise logical shift right
int &= 0xff bitwise AND and assign
int |= 0xff bitwise OR and assign
int ^= 0xff bitwise XOR and assign
int <<= 8 bitwise logical shift left and assign
int >>= 8 bitwise logical shift right and assign
~int bitwise invert

Printing

`puts “Hello World” Prints the string to stdout with a newline
print "prompt> " Prints the string to stdout without a newline
puts obj Converts the object to a String then prints it to stdout
p obj Prints the inspected object to stdout
pp obj Pretty-prints the object to stdout

Statements

while condition
  # ...
end
</td>
until condition
  # ...
end
</td>
if condition
  # ...
end
single_line_statement if condition
Standard if statement.
if condition
  # ...
else
  # ...
end
Standard if / else statement.
if condition
  # ...
eslif other_condition1
  # ...
eslif other_condition2
  # ...
else
  # ...
end
Standard if / else if / else statement.
unless condition
  # ...
end
single_line_statement unless condition
Shorthand for if !condition
case value
when 42
  # ...
when 90..100
  # ...
when 'X'
  # ...
when /[ABC]/
  # ...
when String
  # ...
else
  # ...
end
Standard case statement, but can match by value or by Class. Note: Does not support C-style "fall through".
Standard while loop.
Shorthand for while !condition
loop do
  # ...
end
Infinite loop. Can be broken with break, or return if in a method.

Exception Handling

begin
  # ...
rescue
  # ...
end
Catch all RuntimeError exceptions.
begin
  # ...
rescue ExceptionClass
  # ...
end
Catch any ExceptionClass exception.
begin
  # ...
rescue ExceptionClass => error
  # ...
end
Catch any ExceptionClass exception and save it to the local variable error.
begin
  # ...
rescue ExceptionClass1
  # ...
rescue ExceptionClass1, ExceptionClass2
  # ...
end
Catch different exception classes and handle them differently.

Conversion Methods

obj.to_s convert an Object to a String
int.to_s(16) convert an Integer into a hexadecimal String
int.to_s(7) convert an Integer into an octal String
int.to_s(2) convert an Integer into a binary String
string.to_i convert a String to an Integer
string.to_i(16) convert a hexadecimal String into an Integer
string.to_i(7) convert an octal String into an Integer
string.to_i(2) convert a binary String into an Integer
array.to_set convert an Array into a Set
enum.to_a convert an Enumerator into an Array of elements

Coercion Methods

Integer(obj) convert to an Integer, if obj is not already an Integer
String(obj) convert to a String, if obj is not already a String
Array(obj) convert to an Array, if obj is not already an Array
URI(obj) parse as a URI, if obj is not already a URI object

String Methods

string * 100 repeat the String a hundred times
string << "foo" append directly to the String
string += "foo" create a new String by appending the other String
string[0] return the first character
string[1] return the second character
string[-1] return the last character
string[-2] return the second to last character
string[i,j] get a substring within the String starting at i of length j
string[i..j] get a substring within the String between indexes i and j
string[0] = 'A' sets the first character
string[1,3] = 'AAA' sets multiple characters
string[1..4] = 'AAA' sets multiple characters, using a Range
string.chars return all characters
string.each_char { |c| ... } enumerates over each char in the String
string.each_char return an Enumerator for each char
string.bytes return all bytes
string.each_byte { |b| ... } enumerates over each byte in the String
string.each_byte return an Enumerator for each byte
string.getbyte(0) return the first byte
string.putbyte(0,0x41) sets the first byte
string.strip strips leading and tailing whitespace
string.split splits a String by whitespace
string.split(':') splits a String by a character
string.split(':',2) splits a String, but only returns two elements
string =~ /regex-here/ test if the String matches the Regex
string.match(/regex-here/) get the Regex MatchData back
string.scan(/regex-here/) get an Array of all matching substrings
string.sub('A','X') replace the first occurrence of A with X
string.sub(/[ABC]/,'X') replace the first occurrence of the Regex with X
string.sub(/[ABC]/) { |match| ... } replace the first occurrence of the Regex with whatever the block returns
string.gsub('A','X') replace all occurrences of A with X
string.gsub(/[ABC]/,'X') replace all occurrences of the Regex with X
string.gsub(/[ABC]/) { |match| ... } replace all occurrences of the Regex with whatever the block returns

See the documentation of String for more methods.

Array Methods

array[0] get the first element from an Array
array.first get the first element from an Array
array[1] get the second element from an Array
array[-1] gets the last element in the Array
array[-2] gets the second to last element in the Array
array[0..2] gets the first two elements in the Array
array[-2..] gets the last two elements in the Array
array[0] = 'A' sets the first element in the Array
array[1] = 'B' sets the second element in the Array
array[0,2] = ['A', 'B'] sets the first and second element in the Array
array[0..2] = ['A', 'B'] sets the first and second element in the Array, with a Range
array << 'X' append a single element to the Array
array1.concat(array2) concats another Array to the Array
array.join(' ') joins he elements with a String and returns a String

See the documentation of Array for more methods.

Enumerable Methods

enum.each { |elem| ... } iterate over each element in the Array
enum.find { |elem| elem > 2 } find the first element that matches
enum.map { |elem| elem + 1 } convert an Array into another Array
enum.select { |elem| elem > 2 } select some of the Array’s elements
enum.reject { |elem| elem > 2 } opposite of array.select
enum.grep(/regex-here/) select elements based on the Regex

See the documentation of Enumerable for more methods.

Classes

class Foo
  # ...
end
Define a class called Foo at the top-level.
module Foo
  class Bar
    # ...
  end
end
Define a class called Bar within the namespace Foo.
module Foo
  class Bar

    attr_reader :x, :y

    def initialize(x,y)
      @x = x
      @y = y
    end

  end
end
Define a class called Bar with a initialize constructor method that takes two arguments and sets the @x and @y instance variables.
class Foo < Base
  # ...
end
Define a class called Foo which inherits from another class called Base.
class Base

  attr_reader :x

  def initialize(x)
    @x = x
  end

end

class Foo < Base

  attr_reader :y

  def initialize(x,y)
    super(x)

    @y = y
  end

end
Define a class called Foo which inherits from another class called Base, and defines an initialize constructor which also calls the initialize in the Base class.

Methods

def foo
  # ...
end
Define an instance method called foo that takes no arguments.
def foo(x)
  # ...
end
Defines a method called foo which takes a single argument.
def foo(x,y,z)
  # ...
end
Defines a method called foo which takes three arguments.
def foo(x=1)
  # ...
end
Defines a method called foo which takes a single argument, which defaults to 1 if not given.
def foo(*args)
  # ...
end
Defines a method called foo which takes any number of arguments.
def foo(x: 1)
  # ...
end
Defines a method called foo which takes a keyword argument, which defaults to 1 if not given.
def foo(x: )
  # ...
end
Defines a method called foo which takes a required keyword argument.
def foo(**kwargs)
  # ...
end
Defines a method called foo which accepts arbitrary keyword arguments.
def foo(x)
  yield x + 1
  yield x + 2
  yield x + 3
end
Defines a method called foo which yields multiple times to the given block.
def foo(&block)
  # ...
end
Defines a method called foo which explicitly accepts a block (aka closure, aka anonymous function).
class Foo
  def self.bar
    # ...
  end
end
Defines a "static method" called bar on the class Foo.
module Foo
  def self.bar
    # ...
  end
end
Defines a "static method" called bar on the module Foo.

See Also