Menu Close

Articles

Ruby: Basic concepts

Ruby is an Object Oriented scripting language. It is a high level, dynamic typed language. It was created to improve developer productivity. Today it is mainly used for web back-end services but it was designed as a system scripting language. It is very similar to Python.

Syntax rules

Ruby is case sensitive and has similar naming rules to other languages. You can define identifiers that start with an alphabetic character or underscore. Some identifiers start with capital letters. 

Unlike Python that has mandatory indentation, in Ruby the indentation is optional. Multiple statements can be separated using symbol “;” but this symbol is not required for single line statements.

Unlike other languages that have one single convention for a block of code, Ruby has 2 kind of blocks. For some blocks are enclosed in curly brackets some other blocks are using keywords.

When you write Ruby code you actually create scripts. These are text files with extension *.rb. Most script files start with a special comment: #

First script:

You can run this script using ruby interpreter:

>ruby hello.rb

Example:

This is just a fancy “hello world” script that I will explain next:

Homework: open live demo and run it: hello world

Notes:

  • line of comment start with “#” like in Python,
  • there is no “;” for end of statement,
  • a class is a named block of code like in Python,
  • a method of a class start with keyword “def” like in Python,
  • block of code is ending with “end” like in Pascal,
  • assign is using symbol “=” not “:=” that is used in Pascal, 
  • call of function puts do not require (…), unlike Python,
  • symbol @ is a “sigil” for an attribute,
  • string is enclosed in double quotes: “…”,
  • notation #{@name} is: string interpolation.

Clarification: This is how we learn Ruby in this tutorial. I will show you an example or more, that may be overwhelming at first glance. Then you execute the example once or several times on-line using  repl.it website. Analyze the output an read the notes to understand the new concepts.

Block Comments

In most languages block comments are enclosed in this notation: /* … */. In Ruby this kind of comment is not available. Instead you can use =begin ... =end notation. Block comments are sometimes called “embedded documentation”. It is a good practice to use these kind of comments to explain code purpose.

Example:

Keywords

Most keywords are English words. Some keywords are using uppercase but most of them are using only lowercase letters. Ruby has about 40 keywords that I will introduce step by step using examples.

Expressions

You should be familiar with this concept from mathematics. In Ruby you can use infix expressions. Here is a short reminder: Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is easiest to demonstrate the differences by looking at examples of operators that take two operands:

  1. x + y  : Infix
  2. + x y  : Prefix
  3. x y +  : Postfix

In these expressions x, y are operands while “+” is an operator. The most simple expressions are using one single operator and one operand. For example “-4” is an expression while “4” is not an expression but a single constant literal.  “2 + 4” however is an expression even if there is no variable involved.

Expressions can be combined in larger expressions. Order of execution can be controlled using operator precedence and also round parenthesis. We will investigate this in our next example.

Usually expressions are used in statements. In Ruby stand alone expressions can exist and can represent the result of a function or method. I personally don’t like this approach, it can make the script harder to understand.

Examples

Notes:

  1. In this example we have used 3 numeric operators: { +, /, *}. That should be familiar to you: Addition, division and multiplication. In computer science we have a scarce number of operators due to legacy ASCII notation. { ÷  · } are Unicode symbols for division and multiplication, not available in Ruby.
  2. Square bracket […] and squiggly brackets {…} are used in mathematics to control the order of operations but not in Ruby. Notation [5 + 2] create an array with one element [7] that is replicated into [7, 7]. Totally unexpected result isn’t it?

Homework: Open this example live and run it: ruby expressions

Read next: variables