Ruby Language for Absolute Beginners in 8 Parts

Ruby Language for Absolute Beginners in 8 Parts

Hello!

In this simple, fast and straight to the point Ruby Series, we're going to explore a few Ruby features in 8 Parts!

Do you want to see a spoiler about the 8 Parts?

Don't run away and let's go!

Ruby Language Characteristics

Ruby is awesome and in its heart includes (with many other features) the following characteristics:

Ruby is a Dynamic and Interpreted Programming Language

Your code will be executed at Runtime, by a Virtual Machine, without previous compilation

Write you Ruby code just once, without worrying about the Operating System

Since Ruby is executed by a Virtual Machine, the Ruby code will be generated for the specific Operating System and you don't need to worry about coding with different strategies for each Operating System

Ruby is Object-Oriented Programming Language

Classes and objects are the Ruby heart. This doesn't stop you to have a functional or imperative style if you want

Ruby follows the principles of simplicity and...fun!

Ruby has many built-in methods that allow us to create a powerful, but simple code that is really readable and enjoyable to write.

Ruby is Reflective

You can introspect and modify your code at Runtime, adding more methods in a class for example! Crazy! Powerful feature that will see in another complete post!

Ruby is Open Source

You can collaborate with new features, bug fixes, documentations and much more!

First Ruby Syntax

It's time to create our first Ruby program.

1 - Create the file to be executed by Ruby

Create the file named hello-ruby.rb. This file has the extension .rb

Write the following Ruby code in the file:

2 - Executing the program

To execute the Ruby program just type the following command:

Such emotion! Our first Ruby program has been executed!

In this example we already use our first ruby method, the puts method. Notice that we can omit the parentheses, bringing a simpler way to call a method.

You could write the method in this way if you want:

Executing Ruby Code from IRB

Ruby has its own REPL: Read, Evaluate, Print, Loop. IRB is used for some experiments and probably you'll not use it in production (sometimes just to debug something :P)

Open your terminal (with Ruby installed) and just type:

And the IRB will be ready to be used:

irb(main):001:0>

Let's write the same Ruby code that was written in the hello-ruby.rb, but now in the IRB

Great! The same result!

Exploring Ruby with Docker

If you don't have Ruby installed in your machine you can explore Ruby code using Docker.

Just run the following Ruby container to got into the IRB inside the Docker Container!

Notice that you'll jump into the IRB directly after you launch the container. Pretty simple!

Exploring Ruby Simplicity

Everything in Ruby is an Object! We don't have the concept of primitive types as we have in Java for example.

1 - Comparing Numbers in Ruby

Go again to the IRB and type the following code:

As you can see, the result will be true with this simple comparison. But everything is an object, even a number! So, we can have methods being called in numbers.

Just refactoring the code:

The same result but calling methods on numbers.

2 - Printing 5 times a message

What about printing out a message 5 times? A common code could be:

But in the Ruby World, this code could be better:

Really better, isn't it?

Let's move on!

Ruby Comments

Classical feature in every Programming Language, let's comment a piece of code or explanation just by using the "#" character:

Let's comment multiple lines by using the =begin and =end syntax:

Let's use docstring to do the same thing?

Simple, isn't?

That's it! In the next post: Part 2 - Ruby Methods and Variables we're going to see Methods and Variables in Ruby!

I hope that would be useful to you! Thanks!

Follow us to keep up to date!

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Ruby Beginners Tutorial - Ruby Features and Characteristics - Hacking Code

Part 1 - Ruby Language Characteristics

This is the first Post of the Ruby Series! Let’s see the first Ruby Code using IRB!

Ruby Beginners Tutorial - Ruby Methods - Hacking Code

Part 2 - Ruby Methods and Variables

How can we work with Methods and Variables in Ruby? Let’s see!

Ruby Beginners Tutorial - Ruby Strings - Hacking Code

Part 3 - Ruby Strings

It’s really easy to work with Strings in Ruby! Let’s see how!

Ruby Beginners Tutorial - Ruby Objects - Hacking Code

Part 4 - Ruby Classes and Objects

You can’t work without Classes and Objects in Ruby! It’s time to learn how to create them!

Ruby Beginners Tutorial - Ruby Conditionals - Hacking Code

Part 5 - Ruby Conditionals

Conditionals are important in every Programming Language! But how does that work in Ruby?

Ruby Beginners Tutorial - Ruby Arrays - Hacking Code

Part 6 - Ruby Arrays

Arrays in Ruby is really easy and has a lot of great methods! Check it out!

Ruby Beginners Tutorial - Ruby Hashes - Hacking Code

Part 7 - Ruby Hashes

Hashes in Ruby can be used in different scenarios! Let’s see how to use it!

Ruby Beginners Tutorial - Ruby Loops - Hacking Code

Part 8 - Ruby Loops

Ruby Loops to iterate through Arrays and Hashes is really great! Let’s see why!

0 Comments

Trackbacks/Pingbacks

  1. Ruby Language for Beginners Part 7 – Ruby Conditionals | Hacking Code - […] Part 1 - Ruby Characteristics and first Ruby code […]

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: