Home > Articles > Programming > Ruby

The Ruby Programming Language

You've heard of Perl, but Ruby? This new language from Japan is quickly gaining interest. Here, Ruby's creator, Yukihiro Matsumoto, tells you why Perl and Python just aren't good enough.
Like this article? We recommend

Like this article? We recommend

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.

Smalltalk

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.

Perl

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.

Python

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020