Because everything in the programming world starts with a
Hello World, I’ll come back to the very basics with a Ruby Hello World.
Ruby is an Object Oriented programming language that’s been quite popular in the past years, specially within the Rails framework. I use it on my day to day basis for all sorts of scripting, micro-services and different production tasks.
The thing most people like about Ruby is its simplicity. You can write code quickly as if you’re writing plain text (almost), and you can use the interactive console to test things out.
This article is going to be divided into 3 sections: Installation, Basics and Capabilities. In other posts I’ll go deeper into the more complex and fun characteristics of the language.
Installation is quite simple, nowadays Ruby can be obtained from many different sources and package managers. You can use apt-get, homebrew, download a binary or exec file etc. It will depend on your preference and operating system.
Using a version manager
For me, the best way is to use a version manager. This little tool allows you to have multiple versions of Ruby running seamlessly in the same machine, which is specially useful when having multiple apps, all running different versions (micro-services environment).
The easiest way to install it on MacOS is using
homebrew. You can check other installing options on the chruby website. Depending on your OS, the tools will differ. For Linux users there’s a mirror project called linuxbrew which you can use in a similar fashion than MacOS homebrew.
Now for chruby, simply going to your shell and type:
$> brew install chruby
After that, you can install any version of Ruby you want by using:
$> ruby-install ruby-2.7.2
This will install the ruby version 2.7.2.
This is the absolute basics to get you started. Depending on your particular system configuration, there may be some environment variables to edit or additional small steps. You can test ruby installed correctly by using
ruby -v in your shell.
With chruby correctly installed, you should be now able to switch between installed versions using
$> chruby 2.7.1 for example.
Ruby is a really descriptive programming language. You can find an extensive guide in the official ruby documentation. This article will only highlight certain basic aspects that I consider relevant or interesting.
The easiest way to start playing around with Ruby is using the interactive console (irb — Interactive rb). To access it simply type
irb in your shell. It should already come installed with your ruby version.
Now as promised, the hello world. The
puts command is the equivalent of
>> puts "Hello World!"
Just like that!
Notice that the method itself executes the print, and then it returns a value
nil. This is the special object reserved for
Everything in ruby is an object, even the less intuitive things. That’s something to keep in mind, because this feature will help us accomplish really cool things.
As an example, take the following code:
So far so good.
Now take the following:
=> TrueClass>> 1.class
=> Integer>> "hello".class
What this means is that what we call values in other programming languages, like
hello are objects in Ruby. This allows us to call methods directly on them without having to invoke the parent class
String for example.
=> true>> "hello".size
=> 5>> true.to_s
You may have noticed already that Ruby is also a loosely typed language, there’s no need to declare types on variables; there’s also no need to close the line with a semi-colon for example.
Take the following code:
# Ruby will try to infer the data type based on the value (object) passed
>> a = 100
>> b = 200
>> a + b
=> 300# But it will also let you override these types as you please
# And it will apply validations when appropriate
>> b = "200"
>> a + b
=> TypeError (String can't be coerced into Integer)# Nevertheless, some other times it will try to infer what you want
# Like concatenating Strings
>> a = "100"
>> a + b
=> "100200"# Or transforming ints into floats
>> a, b = 100, 200.0
>> a + b
In case you missed it, Ruby is also capable of doing value destructuring. Check how
a gets assigned the value
b the value
200.0. Ruby has very powerful one-liners. Which is at times a bless, but also a curse. Sometimes it can obfuscate the code, but other times it can save you many lines.
Have a look at a simple variable value swap for example:
>> a = 100
>> b = 200# A typical value swap may include a third temp variable
>> c = a
>> a = b
>> b = c# But in Ruby you can just change them in a one-liner
>> a, b = b, a
There are truly a lot of cool things you can do with the language, and it takes more than a Hello World post to do it, so I’ll cut it short here. I’ll release a series of basic posts aimed at Ruby and other programming languages to discuss the many cool things I encounter in my day to day and comparisons between the different programming languages I use or learn.
I like to believe that every programming language has a series of tasks that it does better than others. You have languages that are great for Math problems, others for Machine Learning, then there’s the ones great to build amazing user interfaces, and the ones to build great distributed systems, etc.
Personally, I put Ruby in the go-to simple category. It’s readable enough to make my code super clean, but powerful enough to be used for a wide variety of applications. Hence I use it as my general purpose programming language.
Within the Rails framework, you can build entire websites really quickly and with little expertise. Once learning the conventions, it becomes an easy process.
For scalable systems, you can use Ruby apps on top of the Sinatra Framework as part of a micro services architecture. This way you can ensure the memory load on particular apps can be managed easily, and you have a good separation of domains, apart from all other advantages you get from a generic micro services system.
I hope you are now interested enough to do a little bit more reading on your own. Ruby is a great language if you’re just getting started with programming or if you’re experienced but looking for clean and readable solutions. It does cover the whole stack.
Apart from being easy to learn, it has a good deal of use cases and a great community behind it, making it easier to learn and maintain.
That’s all for now, happy coding!