Introduction to 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.
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 (www.ruby-lang.org) 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
The Ruby language itself exists as an executable called ruby, which you can run manually by typing it on the command line:
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:
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
- High-Availability Storage with HA-LVM
- DNSMasq, the Pint-Sized Super Dæmon!
- Localhost DNS Cache
- Real-Time Rogue Wireless Access Point Detection with the Raspberry Pi
- Days Between Dates: the Counting
- You're the Boss with UBOS
- The Usability of GNOME
- Linux for Astronomers
- Multitenant Sites
- PostgreSQL, the NoSQL Database