Introduction to Ruby

Everything you need to know to start programming in Ruby.

We programmers are lucky to be working today. I say this because there are so many excellent programming languages from which to choose, especially in the Open Source world.

One of the most talked-about languages is Ruby. Ruby isn't actually all that new. Yukihiro “Matz” Matsumoto released the first public version in 1995, and it has grown in popularity ever since. As the Ruby on Rails framework for Web development has become increasingly popular, interest in Ruby has soared along with it.

Ruby often has been described as a cross between Perl and Smalltalk, and I don't think this is a bad way to look at it. Certainly, if you have experience with both Perl and object-oriented programming, you probably will feel right at home working with Ruby.

In this article, I introduce the basics of Ruby, showing how it is similar to other high-level languages and where it adds its own, special twist. By the end of this article, I hope you'll know enough about Ruby to try it out for some small applications. If you're like me, you'll quickly discover that Ruby is surprisingly compact and elegant, making it possible to write maintainable code quickly and easily.

The Basics

Downloading and installing Ruby is fairly easy, particularly because a recent version (1.8.2) is included with many distributions of Linux. You either can use that version or install the latest version (1.8.4) from the main Ruby site. As an open-source product, you shouldn't be surprised to find that the main Ruby site ( offers the source code in .tar.gz format. Additional formats, such as RPMs and Debs, are available from the official repositories for your favorite distribution.

If you want to install the latest version of Ruby from source, download and unpack the .tar.gz file:

$ cd Downloads
$ tar -zxvf ruby-1.8.4.tar.gz

Now use the standard configure program to find the system configuration automatically, make to compile it and then make test to ensure that the compiled version of Ruby works correctly:

$ ./configure && make && make test

If all goes well, the final line of output from the above commands will read test succeeded. Now you can become the root user and install Ruby onto your system:

$ su
# make install

This installs a variety of Ruby programs and libraries onto your computer.

Interactive Ruby: Irb

The Ruby language itself exists as an executable called ruby, which you can run manually by typing it on the command line:

$ ruby

However, this version of Ruby is designed for non-interactive use. To test code or experiment with the Ruby language, there is irb, the interactive Ruby shell. Irb is something like a debugger, in that it takes input from a user (terminated by pressing the Enter key) and executes it. For example, type:

$ irb

And, irb responds with its prompt:


Now we can type a bit of Ruby:

irb(main):001:0> print "Hello, world"

And, irb responds with:

Hello, world=> nil

The above output indicates that print displays Hello, world on the screen and returns a nil value; nil is Ruby's way of representing a null value, much like undef in Perl, None in Python and NULL in SQL.

Like many other high-level languages, Ruby allows us to assign values to variables without pre-declaring them. Thus, we can write:

greeting = "Hello, world"
print greeting

Ruby also can do math, using the familiar operators +, -, * and /:

5 + 3
60 - 23
60 * 23
10 / 2

I have omitted the call to print in the above lines, because it's unnecessary in irb. However, in a standalone Ruby program, no output would be sent to the screen (or elsewhere) without using print.

If you are a seasoned Perl programmer, you might be somewhat surprised to discover the result of the following:

5 / 2

The above returns 2 because both 5 and 2 are integers, and Ruby assumes you want to perform integer arithmetic. To get a floating-point result, you must ensure that at least one of the numbers is a float:

5 / 2.0



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Inspired by You

noman13bd's picture

Hi, i read this article and install and do some programming with RoR. Here is some of example what i do
Ruby On Rails installation on Windows
Ruby on Rails installation in Ubuntu
autocompleter example in RoR
live Validation for RoR

Ismail Muhammad Noman

I'm only just starting to

Anonymous's picture

I'm only just starting to learn Ruby, but this seems wrong:

"We also can view a subset of the original array by passing two indexes separated by a comma, indicating the first and last index that we want:


Shouldn't it be "indicating the first index and the LENGTH OF THE SUBSTRING that we want"?

Nice and fast intro (plus two typos)

Praxis's picture

I enjoyed this fast intro to Ruby, which quickly points out the basics with nice and clear examples for anyone with some programming experience to easily go hands-on.

I did notice these two typos:

1. When you mention string conversion methods, it is written "You can convert a string to an integer or float using the to_i and to_s methods", but I believe you meant "to_i and to_f"

2. When you give the "id_squared" method example, you mention in the explaining paragraph that it "returns its doubled value", where it should be the "squared value"

Regardless of this, I did enjoy reading this intro.


White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState