For the past few weeks I have been playing around with Ruby, a totally new language for me. I thought that it would be a good idea to list the most important aspects and useful information that I am learning about it and share it with you:
Interpreted language. A program doesn’t need to be compiled previously into machine-language instructions.
Object-oriented language. Everything is an object and every operation is a method call on some object. It is class-based, meaning that objects are instances of classes, which determines their type.
Dynamic language. It executes at runtime common programming behaviours that static programming languages perform during compilation. These behaviours could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system (metaprogramming). It also can ask objects about themselves (reflection).
This is a good example to summarise the definition of a class taken from this course I followed and that I totally recommend if you are starting out with the notion of services:
Attributes Readers & Accessors
In Ruby there is a shortcut to get and set instance variables. It is important to note that attr_accessor is NOT part of the language, but a plain method that uses metaprogramming to create getters and setters for object attributes on the fly.
Iterators let objects manage their own traversal. This tip I read is interesting and worth to remember:
If you’re iterating with an index, you’re probably doing it wrong
I will let you know what’s my experience with it after some time working with the language ☺
Modules & Classes
A module is a collection of methods that aren’t a class, which means that you can’t instantiate it. There are extensively used to mix its methods into a class.
So then, the question would be… how do you choose between a module or a class?
Modules reuse high-level behaviours that could conceptually apply to many classes. Some examples of modules would be: Enumerable or Comparable. Modules use mixin (include Module) as mechanism.
Classes reuse implementation. The subclass will reuse or override any of the superclass methods. The mechanism used to do so is inheritance (class Submodule < Module).
Remarkably often, composition will be preferred over inheritance.
Some useful methods related to orientation of objects:
For sure there is still a lot to be added here, but do you think it is OK for a quick introduction or am I missing something crucial? ☺