Main Features

A modern, interpreted language:

in may aspect Ruby is similar to Python: variables are reference to objects, and their type is defined at run time, when they are assigned. In this way we have a polymorphic language (the same functions can be used for different data types), but the errors on the type of variables are detected only at run-time.

Ruby allows for reflection (the program knows its internals) and metaprogramming (the program can change itself at run-time).

As an interpreted language Ruby is not very efficient and it is not suited for heavy computations; compiled languages perform better. Ruby uses a bytecode to speed things and has a builtin garbage collection system.

Very, very object oriented:

In Ruby all is an object, the object paradigm is very empathized and this leads to a change in the meaning and usage of some classic elements of computer languages: methods and operators become the same thing; no more distinction between basic types and objects, instance variables are used through methods, common keywords become methods of some basic object, loops are replaced by iterator methods:

  • Ruby tries to follow in a strict way the encapsulation paradigm. Class and instance variables are strictly private and accessible only thought special "accessor" methods. These methods, used without parenthesis, seems a direct usage of class variables; instance variables and methods become two very similar things in practical usage.
  • Also basic types (as numbers) are objects and have attributes. Most functions and operators are attributes of objects. Basic operators too are implemented as methods, so the concepts of operator and class method, that are different things in many languages, are unified in Ruby. Operator overloading become a very common feature of classes and an operation not allowed for a given type becomes the missing of a method in the corresponding class.
  • Many features are implemented as methods of basic classes: every class inherits the class Object, which is a common parent for all the Ruby classes. The class Object includes the module "Kernel" with all the major functions of Ruby, and also many statements and operators are implemented as methods of the "Kernel" module
  • In Smalltalk, one of the first object oriented languages, objects are very well separated entities, and calls to methods are seen as "messages" send to objects. In Ruby this idea is not so clearly stated, nevertheless each function is a method of an object which acts as the "receiver" for the function. When the program runs you are always in the environment of an instance, acting as a search scope for names and as a receiver for functions; also in the interactive usage one is in the environment of an instance, which is named: "main" and has inherited Object. This is the way the statements that are methods of the Kernel module are made available to the interactive user. The current instance (the receiver) is referred to by the keyword "self".
  • The class itself (the definition of the class) is built as an instances of the class: Class; class methods are methods of this instance;
  • Only single inheritance is allowed, but a class can include modules, containing attributes that are mixed into the class (mixins).
Duck Typing:

all is an object; also basic types, as integer an floats, are objects: algebraic operators (as addition, multiplication etc.) become methods of the basic objects.

In object oriented languages as C++, or Java, there are some basic types and, in addition, objects, which define new types. But here, with the disappearing of basic operators (now class members), the concept of type weakens, and the object interface becomes the essential item;

This way of intending types is called "duck typing": the object interface defines its type. For the origin of the expression "duck typing" see: http://en.wikipedia.org/wiki/Duck_test .

The syntax tries to mimic the human (English) language:
  • we have postfix syntax, with condition after statements:
  • parenthesis in function calls are optional, so that a function call appears as an actions taken on the arguments;
  • iterator functions associated with blocks seem actions on sets of objects
  • some functions are attributes of basic types, giving a very compact syntax for some common logical constructs: : Es.: 5.times { ...} ;
  • we have functions ending with "=", which mimic an assignment; this choice is related to the need of accessor functions to set variables belonging to classes.
  • return statements in functions are optional: each block of statements return something and acts as an expression, if not specified returns the last evaluated statements, and, if has nothing meaningful to return, returns the nil object.
  • the "then" keyword, used to begin conditional blocks, is optional
  • .

Many find this attempt to make a programming language nearer to the human language a great feature, making Ruby programming: "fun". Solving an interesting problem can be fun, but the used computer language is only a tool: can be easy, powerful, elegant, but if your job is boring no computer language can change things. For me, as an old programmer, programming means translating from the human logic to the computer logic: humans and computers are different and follow different logic schemes, talking to the computer is worthless and essentially wrong. For example I need parenthesis when calling a function, to have an idea of what the computer is doing; in the Ruby world, without parenthesis as delimiters, finding spare words after a function is deceiving and leads to syntactical ambiguities.

Convention over declaration:
  • Constants are capitalized, and also class names.
  • variables beginning with "$","@","@@" are respectively global, instance and class members.
Extended usage of iterators:
there is an extended usage of iterators, implemented as class methods coupled with block of instructions to be executed at each iteration. It's the preferred way of looping in Ruby, with a very compact syntax and an easy way to implement filters and transformations of sequences of objects. Basic types have tenths of different iterators, which can be used for complex tasks. Iterator methods can easily written also for user defined classes.
A lot of funcionalities are built "into" the language:
many features are implemented by means of basic types: common structures, as arrays and associative dictionaries (called hashes in Ruby), are also basic types. There are many operators, a complete set of functions to deal with strings, regular expressions, an easy ways to interact with the underling operating system, an integrated help system, builtin modules for common actions and countless additional packages to do everything.