Thursday, June 7, 2007

Learning Ruby

An Introduction to Ruby

Ruby is a pure object oriented scripting language that runs on most personal computer operating systems including Windows, Linux, Mac OS/X, and BSD. Ruby also runs on most commercial Unix based operating systems as well. Ruby was created in 1995 by Yukihiro Matsumoto, usually referd to as "Matz".

Ruby's popularity grew steadily in the decade following its release. Ruby's growth and popularity are similar to the python language. Both languages have similar goals of simplifying programming. Many programmers have become proficient in both languages, and any perceived 'competition' between the two is usually positive and constructive. Matz mixed together some favorite aspects of other languages he worked with including Perl, Smalltalk, Eiffel, Ada, and Lisp to create Ruby. He wanted a more interactive language that was natural to work in. Something that seemed to 'fit just right'.

I came to ruby late, and through my use of Ruby on Rails in 2005. I've been a proffesional programmer for over a decade, and was most recently building web apps with Java. Rails was like a ray of sunshine in a dark world. The clean, simple approach to programming and the strict adherance to the MVC framework was a great change from the drudgery of configuring large scale Java apps. I was able to pick up Rails pretty quick and become proficcient, but to be better I will need to learn Ruby. So far I am happy to be a 'newbie' in such a great community!

Although I have learned a great deal of Ruby while working with Rails over the last few years, I am going to start over at the begining with Ruby. I've pretty much just picked up Ruby knowledge as I've gone along. I hope that a more rigorous approach to learning Ruby will help me be a better Rails programmer. I'm going to blog about my lessons in Ruby from the start, believing that taking the time to put what I'm learning in writing will help me to understand more.

Most likely I'll just be giving a few readers a nice chuckle at my incompetence.

In an interview with O'Reilly, Matz is quoted as saying:

"I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided to design my own language."

So far Matz, and the Ruby community are meeting that goal.

You can learn much more about Ruby at the Ruby Language site.

Ruby is free software and is available, either, uder the GPL or the Ruby Software License.

Getting started with Ruby

The first step you need to take in learning Ruby is to get Ruby on your machine.

Mac OS X

Mac OS X comes with Ruby pre-installed. If you don't have it, go to the Apple download site.

To install from source, go to the Source section.


Go to the ruby forge website and download the latest
one click installer and run it.


Linux distros usually have ruby installed already. If you need to install it, its best to use your distro's package management system:

Debian Gnu/Linux
 $ apt-get install ruby irb rdoc

RPM distros
Download an RPM from RPM Find and install with
 $ rpm -Uhv ruby-*.rpm
(You can also use something like yum, Yast, or apt4rpm if they are on your distro)

Gentoo Linux
 $ emerge ruby

Go to the Source section.


Ruby is part of the FreeBSD ports collection. You can install it with:

$ cd /usr/ports/lang/ruby

$ make && make install

Source (good for all UNIX)

Get the latest source code for Ruby ruby-1.8.6.tar.gz.

Un-tar the source code

$ tar -xvzf ruby-1.8.6.tar.gz

To install, cd into the resulting library just do the usual three step:

$ configure; make; make install

Your First Ruby Program

Use your favorite text editor to create a file called ruby_hello.rb. Type this one line in the file:

puts 'Hello World!'

save the file and run it at the command line with the command:

$ ruby ruby_hello.rb
Hello World!

You can also run the ruby code directly from the command line with the -e switch, just like in perl:

$ ruby -e "puts 'Hello world'"

Or use irb, the ruby interactive shell:

$ irb
>> puts 'Hello World!'
Hello World!
=> nil

Going back to our text editor, let's add some comments. Ruby single line comments begin with a #. Everything on the line after the # is a comment:

# the classic first program

puts 'Hello World!' #print a greeting

Multi-line comments are rarely used in Ruby. Multi-line or block comments are delimited with '=begin' and '=end':

# the classic first program

puts 'Hello World!' #print a greeting

This ruby program is

© copyrighted by me, 2007.

Or it would be if there

weren't already millions

of other programs exactly

like it, many of which are

in the public domain already.

save the file and run it at the command line and you'll see the exact same result as before:

$ ruby ruby_hello.rb
Hello World!

So that's lesson one. Installing Ruby, writing and running the obligatory 'Hello World!' program to see if the installation went well. If you had any problems, check out the Ruby language page, or join the Ruby community and check in the mailing lists, chat rooms or with the local Ruby Users Group for help.


gazeteler said...

that is wonderfull..

ruby on rails developer said...

nice tutorial for the first time. Have you any guidelines to learn ruby further. thanks