Ruby syntax explained

The syntax of the Ruby programming language is broadly similar to that of Perl and Python. Class and method definitions are signaled by keywords, whereas code blocks can be defined by either keywords or braces. In contrast to Perl, variables are not obligatorily prefixed with a sigil. When used, the sigil changes the semantics of scope of the variable. For practical purposes there is no distinction between expressions and statements. Line breaks are significant and taken as the end of a statement; a semicolon may be equivalently used. Unlike Python, indentation is not significant.

One of the differences from Python and Perl is that Ruby keeps all of its instance variables completely private to the class and only exposes them through accessor methods (attr_writer, attr_reader, etc.). Unlike the "getter" and "setter" methods of other languages like C++ or Java, accessor methods in Ruby can be created with a single line of code via metaprogramming; however, accessor methods can also be created in the traditional fashion of C++ and Java. As invocation of these methods does not require the use of parentheses, it is trivial to change an instance variable into a full function without modifying a single line of calling code or having to do any refactoring achieving similar functionality to C# and VB.NET property members.

Python's property descriptors are similar, but come with a trade-off in the development process. If one begins in Python by using a publicly exposed instance variable, and later changes the implementation to use a private instance variable exposed through a property descriptor, code internal to the class may need to be adjusted to use the private variable rather than the public property. Ruby's design forces all instance variables to be private, but also provides a simple way to declare set and get methods. This is in keeping with the idea that in Ruby one never directly accesses the internal members of a class from outside the class; rather, one passes a message to the class and receives a response.

Interactive sessions

The following examples can be run in a Ruby shell such as Interactive Ruby Shell, or saved in a file and run from the command line by typing ruby ''<filename>''.

Classic Hello world example:

puts 'Hello World!'

Some basic Ruby code:

  1. Everything, including a literal, is an object, so this works:

-199.abs # => 199'ice is nice'.length # => 11'ruby is cool.'.index('u') # => 1"Nice Day Isn't It?".downcase.split().uniq.sort.join

  1. => " '?acdeinsty"

Input:print 'Please type name >'name = gets.chompputs "Hello #."Conversions:

puts 'Give me a number'number = gets.chompputs number.to_ioutput_number = number.to_i + 1puts output_number.to_s + ' is a bigger number.'

Strings

There are a variety of ways to define strings in Ruby.

The following assignments are equivalent:

a = "\nThis is a double-quoted string\n"a = %Qa = %a = %/\nThis is a double-quoted string\n/a = <<-BLOCK

This is a double-quoted stringBLOCK

Strings support variable interpolation:

var = 3.14159"pi is #"=> "pi is 3.14159"

The following assignments are equivalent and produce raw strings:

a = 'This is a single-quoted string'a = %q

Collections

Constructing and using an array:

a = [3, 'hello', 14.5, 1, 2, [6, 15]]

a[2] # => 14.5a.[](2) # => 14.5a.reverse # => 6, 15, 2, 1, 14.5, 'hello', 3]a.flatten.uniq # => [3, 'hello', 14.5, 1, 2, 6, 15]

Constructing and using an associative array (in Ruby, called a hash):

hash = Hash.new # equivalent to hash = hash = # makes the previous line redundant as we are now # assigning hash to a new, separate hash objectputs hash[:fire] # prints "hot"

hash.each_pair do |key, value| # or: hash.each do |key, value| puts "# is #"end

  1. returns and prints:
  2. water is wet
  3. fire is hot

hash.delete :water # deletes the pair :water => 'wet' and returns "wet"hash.delete_if

value

'hot'

# deletes the pair :fire => 'hot' and returns

Control structures

If statement:

  1. Generate a random number and print whether it's even or odd.

if rand(100).even? puts "It's even"else puts "It's odd"end

Blocks and iterators

The two syntaxes for creating a code block:

# note the braces

  1. or:

do puts 'Hello, World!'end

A code block can be passed to a method as an optional block argument. Many built-in methods have such arguments:

File.open('file.txt', 'w') do |file| # 'w' denotes "write mode" file.puts 'Wrote some text.'end # file is automatically closed here

File.readlines('file.txt').each do |line| puts lineend

  1. => Wrote some text.

Parameter-passing a block to be a closure:

  1. In an object instance variable (denoted with '@'), remember a block.

def remember(&a_block) @block = a_blockend

  1. Invoke the preceding method, giving it a block that takes a name.

remember

puts "Hello, #!"
  1. Call the closure (note that this happens not to close over any free variables):

@block.call('Jon') # => "Hello, Jon!"

Creating an anonymous function:

proc

puts arg
Proc.new
puts arg
lambda
puts arg
->(arg) # introduced in Ruby 1.9

Returning closures from a method:

def create_set_and_get(initial_value=0) # note the default value of 0 closure_value = initial_value [Proc.new {|x| closure_value = x}, Proc.new { closure_value } ]end

setter, getter = create_set_and_get # returns two valuessetter.call(21)getter.call # => 21

  1. Parameter variables can also be used as a binding for the closure,
  2. so the preceding can be rewritten as:

def create_set_and_get(closure_value=0) [proc {|x| closure_value = x }, proc { closure_value } ]end

Yielding the flow of program control to a block that was provided at calling time:

def use_hello yield "hello"end

  1. Invoke the preceding method, passing it a block.

use_hello

puts string
# => 'hello'

Iterating over enumerations and arrays using blocks:

array = [1, 'hi', 3.14]array.each

puts item
  1. prints:
  2. 1
  3. 'hi'
  4. 3.14

array.each_index

puts "#: #"
  1. prints:
  2. 0: 1
  3. 1: 'hi'
  4. 2: 3.14
  5. The following uses a (a..b) Range

(3..6).each

puts num
  1. prints:
  2. 3
  3. 4
  4. 5
  5. 6
  6. The following uses a (a...b) Range

(3...6).each

puts num
  1. prints:
  2. 3
  3. 4
  4. 5

A method such as inject can accept both a parameter and a block. The inject method iterates over each member of a list, performing some function on it while retaining an aggregate. This is analogous to the [[foldl]] function in functional programming languages. For example:

[1,3,5].inject(10)

sum + element
# => 19

On the first pass, the block receives 10 (the argument to inject) as sum, and 1 (the first element of the array) as element. This returns 11, which then becomes sum on the next pass. It is added to 3 to get 14, which is then added to 5 on the third pass, to finally return 19.

Using an enumeration and a block to square the numbers 1 to 10 (using a range):

(1..10).collect

x*x
# => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Or invoke a method on each item (map is a synonym for collect):

(1..5).map(&:to_f) # => [1.0, 2.0, 3.0, 4.0, 5.0]

Classes

The following code defines a class named Person. In addition to initialize, the usual constructor to create new objects, it has two methods: one to override the <=> comparison operator (so Array#sort can sort by age) and the other to override the to_s method (so Kernel#puts can format its output). Here, attr_reader is an example of metaprogramming in Ruby: attr_accessor defines getter and setter methods of instance variables, but attr_reader only getter methods. The last evaluated statement in a method is its return value, allowing the omission of an explicit return statement.

class Person attr_reader :name, :age def initialize(name, age) @name, @age = name, age end def <=>(person) # the comparison operator for sorting @age <=> person.age end def to_s "# (#)" endend

group = [Person.new("Bob", 33), Person.new("Chris", 16), Person.new("Ash", 23) ]

puts group.sort.reverse

The preceding code prints three names in reverse age order:

Bob (33)Ash (23)Chris (16)

Person is a constant and is a reference to a Class object.

Open classes

In Ruby, classes are never closed: methods can always be added to an existing class. This applies to all classes, including the standard, built-in classes. All that is needed to do is open up a class definition for an existing class, and the new contents specified will be added to the existing contents. A simple example of adding a new method to the standard library's Time class:

  1. re-open Ruby's Time class

class Time def yesterday self - 86400 endend

today = Time.now # => 2013-09-03 16:09:37 +0300yesterday = today.yesterday # => 2013-09-02 16:09:37 +0300

Adding methods to previously defined classes is often called monkey-patching. If performed recklessly, the practice can lead to both behavior collisions with subsequent unexpected results and code scalability problems.

Since Ruby 2.0 it has been possible to use refinements to reduce the potentially negative consequences of monkey-patching, by limiting the scope of the patch to particular areas of the code base.

  1. re-open Ruby's Time class

module RelativeTimeExtensions refine Time do def half_a_day_ago self - 43200 end endend

module MyModule class MyClass # Allow the refinement to be used using RelativeTimeExtensions

def window Time.now.half_a_day_ago end endend

Exceptions

An exception is raised with a raise call:

raise

An optional message can be added to the exception:

raise "This is a message"

Exceptions can also be specified by the programmer:

raise ArgumentError, "Illegal arguments!"

Alternatively, an exception instance can be passed to the raise method:

raise ArgumentError.new("Illegal arguments!")

This last construct is useful when raising an instance of a custom exception class featuring a constructor that takes more than one argument:

class ParseError < Exception def initialize(input, line, pos) super "Could not parse '#' at line #, position #" endend

raise ParseError.new("Foo", 3, 9)

Exceptions are handled by the rescue clause. Such a clause can catch exceptions that inherit from StandardError. Other flow control keywords that can be used when handling exceptions are else and ensure:

begin # do somethingrescue # handle exceptionelse # do this if no exception was raisedensure # do this whether or not an exception was raisedend

It is a common mistake to attempt to catch all exceptions with a simple rescue clause. To catch all exceptions one must write:

begin # do somethingrescue Exception # Exception handling code here. # Don't write only "rescue"; that only catches StandardError, a subclass of Exception.end

Or catch particular exceptions:

begin # do somethingrescue RuntimeError # handle only RuntimeError and its subclassesend

It is also possible to specify that the exception object be made available to the handler clause:

begin # do somethingrescue RuntimeError => e # handling, possibly involving e, such as "puts e.to_s"end

Alternatively, the most recent exception is stored in the magic global $!.

Several exceptions can also be caught:

begin # do somethingrescue RuntimeError, Timeout::Error => e # handling, possibly involving eend

Metaprogramming

Ruby code can programmatically modify, at runtime, aspects of its own structure that would be fixed in more rigid languages, such as class and method definitions. This sort of metaprogramming can be used to write more concise code and effectively extend the language.

For example, the following Ruby code generates new methods for the built-in String class, based on a list of colors. The methods wrap the contents of the string with an HTML tag styled with the respective color.

COLORS =

class String COLORS.each do |color,code| define_method "in_#" do "

" end endend

The generated methods could then be used like this:

"Hello, World!".in_blue => "

Hello, World!"

To implement the equivalent in many other languages, the programmer would have to write each method (in_black, in_red, in_green, etc.) separately.

Some other possible uses for Ruby metaprogramming include: