The Ruby Programming Language
- The Ruby Programming Language
- Brief Summary of Ruby Language
- Sample Programs
- How to Obtain Ruby
- Conclusion: Solving Everyday Tasks
Ruby is a powerful and dynamic open source, object-oriented language that I began developing in 1993. Ruby runs on many platforms, including Linux and many flavors of UNIX, MS-DOS, Windows 9x/2000/NT, BeOS, and MacOS X.
Ruby's primary focus is productivity of program development, and users will find that programming in Ruby is productive and even fun. Ruby is well suited for the problem domains such as these:
Text processing—Ruby's File, String, and Regexp classes help you process text data quickly and cleanly.
CGI programming—Ruby has everything you need to do CGI programming, including text-handling classes, a CGI library, database interface, and even eRuby (embedded Ruby) and mod_ruby for Apache.
Network programming—Network programming can be fun with Ruby's well-designed socket classes.
GUI programming—GUI tool kit interfaces such as Ruby/Tk and Ruby/Gtk are available.
XML programming—Text-handling features and the UTF-8-aware regular expression engine make XML programming handy in Ruby. The interface to the expat XML parser library is also available.
Prototyping—With its high productivity, Ruby is often used to make prototypes. Prototypes sometimes become production systems by replacing the bottlenecks with C written extensions.
Programming education—You can teach students that programming is fun
Now are you more interested in Ruby? You'll find out more in this article. Ruby is named after the red jewel; it's not an acronym. I chose the name of the language from the jewel name, influenced by Perl. Although I named Ruby rather by coincidence, I later realized that ruby comes right after pearl in several situations, including birthstones (pearl for June, ruby for July) and font sizes (Perl is 5-point, Ruby is 5.5-point). I thought that it only made sense to use Ruby as a name for a scripting language that was newer (and, hopefully, better) than Perl.
Ruby has adopted various features from many languages, including Perl, Lisp, and Smalltalk, and it has become a different language than the others. Let me sketch Ruby by comparing it with other languages.
Like Smalltalk Ruby is a dynamic and pure object-oriented language. Both languages are dynamic because they do not use static type information. They are pure because all values are objects and are classified into classes that are objects themselves. Both are also designed to be object-oriented languages from the beginning, and they both support garbage collection.
In Smalltalk, control flow structures such as conditionals are done by sending messages to the objects—at least, that's how it appears. Sometimes this makes Smalltalk programs unnatural and hard to read.
In Ruby, control flow structure is far more conservative. Smalltalk is an operating system and a programming environment. The program is basically an image within the environment that is constructed through interaction via browsers. Unlike Smalltalk programs, Ruby programs are clearly separated from the language and its interpreter.
Ruby and two other great "P" languages (Perl and Python) often are classified as scripting languages. They are scripting languages, but probably not in the sense that you imagine. They are scripting languages for these reasons:
They support a fast development cycle (edit-run-edit) by interpreters. No compilation is needed.
They focus on quick programming by requiring you to code less. For example, you don't have to deal with static types of variables. Fewer declarations are needed in programs. Because of these attributes, these languages can be used for everyday task one-liners. Imagine developing a so-called one-liner (such as scanning the log files) in C, for example.
A strong set of built-in libraries supports the handling of text and files.
Unfortunately, by the word scripting, many people imagine poor languages that can be used only for small programs. That was true in the past and is still true for some languages, such as csh. After Perl, scripting languages are languages that focus on quick development, although Perl still has the smell of old scripting attributes. Even if you can't seem to throw off this illusion, do not call Ruby a scripting language; instead, call it a "dynamic object-oriented language."
Unlike Perl, Ruby is a genuine object-oriented language; OOP features are not an add-on. Ruby uses less punctuation ($,@,%, and so on), less context dependency, and less implicit type conversion, so Ruby programs tend to be less cryptic.
For example, the following is used in Ruby to obtain length of a string and an array:
a = "abc" a.length # => 3 a = [1,2,3] a.length # => 3
Very simple. In Perl, however, things are far more complicated:
$a = "abc"; length($a); # => 3, it's OK @a = (1,2,3); length(@a); # => 1, not as expected scalar(@a); # => 3, it's the Perl way to get array size $a = [1,2,3]; # reference to an anonymous array length($a); # => 16, not as expected scalar(@$a); # => 3, need dereference to get array size
You must always be aware of data types and context in Perl, and this can be a burden for programmers. Ruby frees you of this burden.
In Ruby, most of the Perl functions are organized into class libraries. Simple Ruby programs often look like reordered and simplified Perl programs. Take a look at some examples:
-- Ruby print "hello world" -- -- Perl print "hello world"; -- -- Ruby print Time.now.strftime("%a %b %e %H:%M:%S %Y\n") -- -- Perl use POSIX qw(strftime); print strftime("%a %b %e %H:%M:%S %Y\n", localtime(time())); -- -- Ruby require 'socket' print TCPSocket.open("localhost", "daytime").read -- -- Perl use IO::Socket; $sock = IO::Socket::INET->new(PeerAddr => 'localhost:daytime'); print <$sock>; --
Ruby is very much influenced by Perl—in fact, some users describe Ruby as "a better Perl than Perl." I believe that I have removed most of the Perl traps from Ruby, although a few new ones may have been added.
On the Python newsgroup, questions/requests/complaints such as the following seem to crop up from time to time:
I dislike code structuring by indentation.
Why doesn’t Python have a "real" garbage collection?
Why are there two distinct data types, list and tuple?
Separating types and classes is annoying. Why are all values not class instances?
Why is no method available for numbers, tuples, and strings?
Explicit conversion between small integers and long integers is annoying.
Maintaining reference counts in the extensions is tiresome and error-prone.
Of course, the above are not always problems. Many Pythoneers live happily with these attributes of Python, and some even consider them features. I don't think that most of them will be removed from a future Python, but all of these are already solved in Ruby. From my point of view, I have provided "a better Python than Python."
Because Ruby supports a strong set of functions that are designed after Perl, Ruby programs tend to be smaller and more concise than ones in Python. Ruby programs also often run faster than their Python equivalents, partly because the Ruby interpreter uses the method-cache technique.
Ruby is bigger than Python in many ways as well, including syntax. But, from my point of view, it makes programs more natural. Here's an interesting quote from Programming Perl by Larry Wall:
- Minimalism: The belief that "small is beautiful." Paradoxically, if you say something in a small language, it turns out big, and if you say it in a big language, it turns out small. Go figure.
Ruby is hovering near the edge of too complicated.
Design Policy of Ruby
For me, the purpose of life is, at least partly, to have joy. Programmers often feel joy when they can concentrate on the creative side of programming, so Ruby is designed to make programmers happy. I consider a programming language as a user interface, so it should follow the principles of user interface.
Principle of Conciseness
I want computers to be my servants, not my masters. Thus, I'd like to give them orders quickly. A good servant should do a lot of work with a short order.
Principle of Consistency
As with uniform object treatment, as stated before, a small set of rules covers the whole Ruby language. Ruby is a relatively simple language, but it's not too simple. I've tried to follow the principle of "least surprise." Ruby is not too unique, so a programmer with basic knowledge of programming languages can learn it very quickly.
Principle of Flexibility
Because languages are meant to express thought, a language should not restrict human thought, but should help it. Ruby consists of an unchangeable small core (that is, syntax) and arbitrary extensible class libraries. Because most things are done in libraries, you can treat user-defined classes and objects just as you treat built-in ones.
Programming is incredibly less stressful in Ruby because of these principles.