Getting started in Ruby

January 5th, 2008  | Tags:

Although this is a Java blog, I am going to get started write about Ruby as well.

Why? Because I am studying Ruby and I am very impressed with this language. Also, with Ruby running on JVM (JRuby) I see a big opportunity to use both languages in a great environment.

In the past, I have studied Perl (where I have used at IBM) and also Python. But Ruby has been the most fantastic script language I have ever seen.

For this first topic, I am going to show how to install Ruby on Linux Ubuntu 7.10, as well as see its documentation through the shell.

Installing Ruby on Ubuntu 7.10

Fortunately this is an easy process. Simply type the following command on the linux shell:

sudo apt-get install ruby rdoc ri irb libyaml-ruby libzlib-ruby ri ruby1.8-dev

The command above is going to install Ruby, IRB tool, some libs and its documentation.

Next, we are going to install the gems. Follow the following commands:

wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz
tar zxvf rubygems-1.0.1.tgz
cd rubygems-1.0.1
sudo ruby setup.rb
sudo gem update --system
  1. Download gem source code
  2. Unzip the file
  3. Get into rubygems directory
  4. Install gem
  5. Update gem

Simple, isn’t it?

After that Ruby should be installed. To make a test, type irb in the shell. After, type puts “Hello World”. You are going to see a screen like that:

As you can see, irb is a tool where you are able to write Ruby code. It is useful when you are testing and/or learning Ruby.

Hello Ruby World

The example above is a “Hello World” using IRB. If you want to create a concrete file, simply create a file with .rb extension and insert the same code puts “Hello World”.

After that, on the shell, type ruby <file name>.rb. Done!! you have created a concrete file and ran it.

Documentation

Documentation is our friend in any language. In Ruby, you can read the API documentation through ri tool.

If you want to read the documentation about any class, for instance an Array, simply type on shell:

ri Array

A documentation about Array will appear on your screen.

If you want to read the documentation about a certain method, for instance, a each method from array, type on the shell:

ri Array.each

This is a useful tool and you should use it as much as you can.

Well guys, I hope this simple topic be useful for anyone. Next, I am going to show you how to improve the VIM editor to create/edit ruby files.

See you :)

  1. January 8th, 2008 at 15:09
    #1

    Or you can use Netbeans and have JRuby out of the box.
    Also use the latest Gem available, they just released 1.0 and now 1.0.1.

  2. jrjuniorsp
    January 8th, 2008 at 16:01
    #2

    Hey Piku
    Thank you very much for your comment.
    I just updated my gems on my local computer and also I’ve updated the blog.

TOP