Ruby Language for Beginners in 8 Parts

DZone 's Guide to

Ruby Language for Beginners in 8 Parts

In Part 1 of this series, a web developer shows us how to get started with the Ruby language by explaining its syntax, how to use the IRB, and more.

· Web Dev Zone ·
Free Resource


In this simple, fast, and straightforward 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?

  • Part 1 - Ruby Characteristics and first Ruby code

  • Part 2 - Ruby Methods and Variables

  • Part 3 - Ruby Strings

  • Part 4 - Ruby Classes, Objects, and Instances

  • Part 5 - Ruby Conditionals

  • Part 6 - Ruby Arrays

  • Part 7 - Ruby Hashes

  • Part 8 - Ruby Loops

Don't run away and let's go!

Ruby Language Characteristics

Ruby is awesome, and, at its core, 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 your 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 an Object-Oriented Programming Language.

Classes and objects are at the heart of Ruby. This doesn't stop you from having 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 powerful, but simple code that is really readable and enjoyable to write.

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

You can collaborate with new features, bug fixes, documentation, 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:

puts "Ruby Rocks!"

2 - Executing the program.

To execute the Ruby program just type the following command:

$ ruby hello-ruby.rb

Ruby Rocks!

Such emotion! Our first Ruby program has been executed!

In this example, we already used 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:

puts("Ruby Rocks!")

Executing Ruby Code From IRB

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

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

$ irb

And the IRB will be ready to be used:


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 on your machine, you can explore Ruby code using Docker.

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

$ docker run -it --rm --name ruby-container ruby

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 to the IRB again and type the following code:

irb(main):001:0> 5 == 5
=> true

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:

irb(main):001:0> 5.eql? 5
=> true

We get the same result but, here, we're calling methods on numbers.

2 - Printing a message 5 times.

What about printing out a message 5 times? A common way of executing this code would be:

count = 5
while count > 0
    puts "My message"
    count = count - 1

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

5.times { puts "My message" }

Really better, isn't it?

Let's move on!

Ruby Comments

A classical feature in every programming language, let's comment out a piece of code or explanation just by using the "#" character:

# This method is awesome, it doesn't need parentheses!
puts "Ruby Rocks!"

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

Even with more than one argument,
a method in Ruby can be called without parentheses
Such great, Ruby!

Let's use docstring to do the same thing:

Even with more than one argument,
a method in Ruby can be called without parentheses
Such great!

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!

Don't forget to explore all the Ruby Series :)

I hope this has been useful to you! Thanks!

ruby, ruby classes, ruby developrs, ruby objects, web dev

Published at DZone with permission of Alexandre Gama . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}