Have you ever heard about Ruby? It's my favourite programming language. It was invented in Japan, which is probably the most amazing country in the world, and I think no language can beat Ruby when it comes to magic. Yes: Ruby is Magic.
You don’t believe me, do you? Fair enough, but let me show you why Ruby is so awesome.
You can read and write it like English.
Yes, it’s true. Ruby is so simple and intuitive that you can think in English before writing some Ruby code. For example:
def speak_english
print "Hello, world!"
end
The code above is a Ruby function (or method) declaration. So, when you want to run the speak_english
function, you do it this way:
speak_english
Hello, world! => nil
You may have noticed the nil
: what’s that? It’s just nothing, literally. It represents the void (emptiness, no value at all). In other languages, such as SQL (the mother tongue of databases), you can find it as NULL
.
In Ruby, everything is an object.
Do you know about OOP? It means Object-Oriented Programming, and it’s probably the most important programming paradigm ever invented so far. Ruby takes full advantage of OOP. And when I say full, I literally mean: everything, in Ruby, is an object. Even numbers! If you know at least one programming language, say Java, you must be aware of the fact that Java numbers are primitive types, which mean that they’re not objects. In Ruby, things are different.
Let’s make an example. Let’s say you want to use the speak_english
function 3 times. In Java, you’d do something like:
public class HelloWorld
{
public static void main(String[] args)
{
for (int i = 0; i < 3; i++)
{
speakEnglish();
}
}
public static void speakEnglish()
{
System.out.println("Hello, world!");
}
}
So much code for something so simple… in Ruby, instead, you can do this:
3.times do
speak_english
end
Hello, world! Hello, world! Hello, world! => 3
See? I called a function on a number! Cool, isn’t it? And I used only 3 lines of code :-)
Ruby is clear, concise and understandable.
I was a PHP developer when I discovered Ruby. Although I had a bit of OOP background, I was used to write PHP code in a procedural style. Procedural code looks something like:
doThis();
doThat();
doSomethingElse();
There’s absolutely nothing wrong with this approach, apart from the fact that it starts being cumbersome, sometimes… because it’s not Object-Oriented. I’ll make one last example, taken from a beautiful StackOverflow’s answer.
Reverse the words in this string:
backwards is sentence This
So the final result must be:
This sentence is backwards
When you think about how you would do it, you’d do the following:
- Split the sentence up into words
- Reverse the words
- Re-join the words back into a string
In PHP, you’d do this:
$sentence = "backwards is sentence This";
$splitted = explode(" ", $sentence);
$reversed = array_reverse($splitted);
$rejoined = implode(" ", $reversed);
In Python:
sentence = "backwards is sentence This"
splitted = sentence.split()
reversed = reversed(splitted)
rejoined = " ".join(reversed)
And Ruby:
sentence = "backwards is sentence This"
splitted = sentence.split
reversed = splitted.reverse
rejoined = reversed.join
Every language required 4 lines of code. Now let’s compare the one-liners.
PHP
implode(" ", array_reverse(explode(" ", $sentence)));
Python
" ".join(reversed(sentence.split()))
Ruby
sentence.split.reverse.join " "
Now, can you see the beauty of Ruby? It’s just… magic.