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 |
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 |
@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 |
def initialize |
Initializer method This method is called whenever you create an instance. By
default you create an object with |
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 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 |
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. |
Strings can use either single or double quotes (with different escaping rules).