DEV Community

Ron
Ron

Posted on

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, brew, download a binary or exec file etc.

But 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](https://github.com/postmodern/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 is using brew. You can check other installing options on their website. Depending on your OS, the tools will differ.

Now for chruby, simply going to your shell and type:

$> brew install chruby
Enter fullscreen mode Exit fullscreen mode

After that, you can install any version of Ruby you want by using:

$> ruby-install ruby-2.7.2
Enter fullscreen mode Exit fullscreen mode

This will install the version 2.7.2.

This is the absolute basics. You can test ruby installed correctly by using ruby -v

You can also 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. I 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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Just like that!

Notice that the method itself executes the print, and then it returns a value nil. nil is the special object reserved for NULL .

Now, everything in ruby is an object, even the less intuitive things. That's something to keep in mind, because this approach will help us accomplish really cool things.

As an example, take the following code:

>> nil.class
=> NilClass

>>nil.nil?
=> true
Enter fullscreen mode Exit fullscreen mode

Pretty straightforward, right?

Now take the following:

>> true.class
=> TrueClass

>> 1.class
=> Integer

>> "hello".class
=> String
Enter fullscreen mode Exit fullscreen mode

What this means is that what we call values in other programming languages, like true, 1 and hello are objects in Ruby. This allow 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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 others 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
Enter fullscreen mode Exit fullscreen mode

There are truly a battalion 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 have 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 full 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 I've interested you 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 spectrum.

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!

For more topics, check my profile or visit rarias.dev.

Top comments (0)