Hello World — Ruby

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.

Many languages have adopted these practices and now it’s even easier to get started with them as well. The likes of JavaScript, Python, PHP, etc have come a long way too.

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

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.

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).

My favourite one is chruby. There’s also other popular ones like rvm or rbenv but I found chruby works the best for me.

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.

Basics

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.

$> irb
irb(main):001:0>

Now as promised, the hello world. The puts command is the equivalent of print in other languages. It simply outputs the object that follows.

>> puts "Hello World!"
Hello World!
=> nil

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 NULL .

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:

>> nil.class
=> NilClass
>>nil.nil?
=> true

So far so good.

Now take the following:

>> true.class
=> TrueClass
>> 1.class
=> Integer
>> "hello".class
=> String

What this means is that what we call values in other programming languages, like true, 1 and hello are objects in Ruby. This allows us to call methods directly on them without having to invoke the parent class Integer or String for example.

>> 1.odd?
=> true
>> "hello".size
=> 5
>> true.to_s
=> "true"

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
=> 300.0

In case you missed it, Ruby is also capable of doing value destructuring. Check how a gets assigned the value 100 and 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.

Capabilities

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.

Conclusion

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!

Software Engineer in the UK, also tech enthusiast. Interested in problem solving and public speaking.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store