Ruby is a purely object-oriented programming language originally developed for scripting. It combines Perl-like syntax with Smalltalk-like semantics, and also shares some features with Python, Lisp and CLU.
Ruby currently has only one implementation, the Ruby interpreter, although efforts are underway to implement a Ruby front end (called "Cardinal") for the Parrot virtual machine.
Ruby language features:
- Perl-like syntax
- Object-oriented features:
- Operator overloading
- Exception handling
- No Unicode support
- Iterators (which resemble those in CLU and Sather)
- Closures (also found in Smalltalk and many functional programming languages)
- Syntactic support for Perl-like regular expressions at the language level (not merely in libraries, as in Python or many other languages)
- Garbage collection
- Dynamic library loading/linking (depending on the architecture)
- High portability (runs on Unix, Microsoft Windows, DOS, Mac OS X, OS/2, Amiga, and many more)
- Distributed under Free and open source licences (GPL or Artistic License).
Ruby is purely object-oriented: every bit of data is an object, including types that are designated "primitive" in impure languages. Every function is a method. This is similar to Smalltalk but unlike Java and Python. Every named value (variable name) in a Ruby program designates a reference to an object, not the object itself.
The language was created by Yukihiro Matsumoto (a.k.a. "Matz") on February 24, 1993. The current stable version is 1.8.0. Note that the name is not an acronym--it is actually a pun on Perl. According to the author, he designed Ruby to follow the principle of least surprise (POLS), meaning that the language should be free from the traps and inconsistencies that plague other languages.
From the Ruby FAQ: If you like Perl, you will like Ruby and be right at home with its syntax. If you like Smalltalk, you will like Ruby and be right at home with its semantics. If you like Python, you may or may not be put off by the huge difference in design philosophy between Python and Ruby/Perl.
Examples
Here is some exsamples of Ruby code:
>> -199.abs # The number -199 is an object; the method abs is called. => 199
>> "Ruby is cool".length # length is a method of String objects => 13
>> "Rick".index("c") => 2
>> "John".swapcase => "jOHN"
>> #Arrays ?> [11, 5, 7, 2, 13, 3].sort => [2, 3, 5, 7, 11, 13]
>> [11, 5, 7, 2, 13, 3].sort.reverse => [13, 11, 7, 5, 3, 2]
# Execute the following block of code 10 times 10.times { # Replace ' ' with ', ' and store in string1 string1 = "Hello world".gsub(" ", ",")
# append "!" to variable 'string1' string1 += "!"
# print variable 'string1', followed by a newline puts string1 }
More Ruby code is available in form of sample algorithm implementations in the articles: