Features of Ruby language
I have listed few prominent features of Ruby that one should aware.
Nearly Pure Object Oriented
Almost everything is an object making it purely Object Oriented Programming language. Each and every value is an object. Every object has a class and every class has a super class. Every object has their properties and actions.
Dynamic typing and Duck typing
Ruby is a dynamic programming language and its programs are not compiled. The type of any datatype is determined at runtime.
Ruby variables are loosely typed language which means any variable can hold any type of object. When a method is called on an object, Ruby only looks up at the name irrespective of the type of object. This is duck typing. It allows you to make classes that pretend to be other classes.
If it walks like a duck and it quacks like a duck, then it must be a duck - to determine if an object can be used for a particular purpose. With normal typing, suitability is determined by an object’s type. In duck typing, an object’s suitability is determined by the presence of certain methods and properties, rather than the type of the object itself.
Flexibility
Ruby is a flexible language.
It allows you to easily remove, redefine or add existing parts to it.
It allows its users to freely alter its parts as they wish.
One can modify a class, e.g., one can open the Integer
class and redefine the implementation of +
into multiplication operation.
Metaprogramming
Ruby allows you to operate on the programs as the data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself while running. In some cases, this allows programmers to minimize the number of lines of code to express a solution, in turn reducing development time. It also allows programs greater flexibility to efficiently handle new situations without recompilation.
Mixins
Ruby has a feature of single inheritance only. Ruby has classes as well as modules. A module has methods but no instances. Instead, a module can be mixed into a class, which adds the method of that module to the class. It is similar to inheritance but much more flexible.
Natural English-like Appearance
The Ruby program prefers English keyword and some punctuation for decoration. It doesn’t need variable declaration, e.g.:
if 5 > 2 then print "Good" else print "bad" end
Naming Conventions
Ruby defines some naming conventions for its variable, method, constant and class.
- Constant: Starts with a capital letter
- Global variable: Starts with a dollar sign ($)
- Instance variable: Starts with a (
@
) sign - Class variable: Starts with a (
@@
) sign - Method name: Allowed to start with a capital letter
Default Arguments
A default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full (like in C).
Special Method Names
Methods are allowed to end with question mark (?
) or exclamation mark (!
).
By convention, methods that answer questions with an answer that can be used as a Boolean end with question mark.
Methods that change the state of the object it is called on, or modifies an object passed in, can indicate that “danger” by having an exclamation mark at the end of that method name.
Statement delimiters
We can have multiple statements on a single line separated by a semicolon (;
).
We can end a statement with a semicolon on a single line, but this is unnecessary, and rarely seen.
Interactive Ruby (IRB)
IRB is Ruby’s REPL (Read, Eval, Print, Loop).
We presume the abbreviation irb comes from the fact that the filename
extension for Ruby is .rb
. “Interactive RuBy”.
The program is launched from a command line and allows the execution of Ruby commands with immediate response, experimenting in real-time. It features command history, line editing capabilities, and job control, and is able to communicate directly as a shell script over the Internet and interact with a live server. It was developed by Keiju Ishitsuka.
Rubygems
All the packages of Ruby are managed via Rubygems. A gem is nothing but a package.
Major OS Support
Ruby is available in many of the popular operating systems today.
Garbage Collection
Ruby support garbage collection of unused objects automatically. Programmers need not to release objects manually. The strategies of Garbage collection has evolved from Mark & Sweep to Incremental Sweep in Ruby 2.2. More can be found here.