This is a brief guide to help you to read ruby code examples on my site. This is not a complete guide to ruby, I only cover constructs that I use in my articles. I assume you're familiar with a basic OO language.

The left hand colum contains fragments of code, the right side is explanation of what that group of fragments means. If you find some ruby you don't follow, scan down the left to see something similar.

For an introduction to ruby, the best book is the PickAxe. For an online guide, there is nothing quite like Why's (poignant) Guide.

puts “hello world”

Print to Console

Strings can use either single or double quotes (with different escaping rules).

class ClassName

Declares a class

def some_method arg1, arg2
  call_method(arg1)
end

def property= arg1
  @property = arg1
end

Declares a method

The method may be global (if defined at the top level) or an instance method on a class.

Parentheses are optional on method definition and method call, providing there is no ambiguity.

Method names may include “=” (in which case they are used as name = aValue. Names may include “?”, commonly used for boolean getters (eg aSet.include?(aValue)).

@field

Instance variable.

Does not have to be declared, it is defined on first used. If accessed before set it returns nil.

def self.class_method

Define a class method

@@class_variable

Class Variable

$global_variable

Global Variable

attr_accessor :field1, :field2
attr_reader :field

Instance variable with accessors

Uses run-time code generation to define getter name and setter name = arg. attr_accessor defines both, attr_reader only defines a getter.

def initialize

Initializer method

This method is called whenever you create an instance. By default you create an object with aClass.new(arg1, arg2). Both arguments are passed to initialize, so you declare an argument for each argument you'll need. You may also define other class methods that create new instances with other names.

class Subclass < Superclass

Declare inheritance

{|arg1, arg2| doSomething(arg1, arg2)}

do |arg1, arg2| 
  doSomething(arg1, arg2)
end

do
  doSomething
end

Block

Defines a block, Ruby's name for a Closure. The two syntaxes {} and do/end are mostly equivalent. The {} form is usually used for one-liners and the do/end form for multi-line blocks.

May take none or many arguments. Like any closure can refer to any variable within lexical scope.

Blocks are widely used in Ruby throughout the libraries.

var ||= value

Assign if Nil

Assigns value to var only if var is nil. It's the equivalent of var = value if nil == var.

The gory detail here comes from the fact that nil is falsy (meaning nil is treated as false in boolean expressions). So similarly to expressions like var += 5, this one is equivalent to var = var || value

aList.each {|i| doSomething(i)}

Loop through a list

A block is the usual way to loop through a list. Other operations on a list are done with CollectionClosureMethods.

emptyArray = []
newArray = [var, “some text”]
doSomething([var1, var2])

Array

Ruby's arrays are really lists. They are growable structures with a rich API. You can freely use them anywhere in expressions, such as in a method call.

var = aHash[key]
aHash[key] = var
newHash = {key1 => value1, key2 => value2}
emptyHash = {}

Hash

anObject.
  aMethod(arg1,
    arg2,
    arg3)

Inferred Statement Continuation

Ruby uses newlines as statement delimiters. 96% of the time this is exactly what you need. Occasionally, however, lines get to be too long, so langauges that use newlines need a statement continuation character to allow the statement to span lines.

Ruby uses several characters to infer statement continuation. So if you finish a line with a “,” in an argument list, ruby infers that you want to continue the statement. Similarly if you end a line with a “.” for a method call.