Wondering why Ruby is so popular? Fans call it beautiful and artful, yet also handy and practical. What gives?
The Ideals of Ruby's Creator
Ruby is a language of careful balance. Its creator, Yukihiro "Matz" Matsumoto, blended parts of his favorite languages (Perl, Smalltalk, Eiffel, Ada, and Lisp) to form a new language that balanced functional programming with imperative programming.
He has often said that he is "trying to make Ruby natural, not simple," in a way that mirrors life.
Ruby is simple in appearance, but is very complex inside, just like our human body1.
About Ruby's Growth
Since its public release in 1995, Ruby has drawn devoted coders worldwide. In 2006, Ruby achieved mass acceptance. Active user groups formed in the world's major cities and Ruby-related conferences filled to capacity.
Ruby-Talk, the primary mailing list for discussion of the Ruby language, climbed to an average of 200 messages per day in 2006.
Ruby is also completely free. Not only free of charge, but also free to use, copy, modify, and distribute.
Everything is an Object
Initially, Matz looked at other languages to find an ideal syntax. Recalling his search, he said, "I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python3."
In Ruby, everything is an object. Every bit of information and code can be given their own properties and actions. Object-oriented programming calls properties instance variables and actions methods. Ruby's pure object-oriented approach is most commonly demonstrated with a bit of code which applies an action to a number:
5.times { print "We *love* Ruby -- it's outrageous!" }
In many languages, numbers and other primitive types are not objects. Ruby follows the influence of the Smalltalk language by giving methods and instance variables to all of its types.
Ruby's Flexibility
Ruby is seen as a flexible language, since it allows its users to freely alter its parts. Essential parts of Ruby can be removed or redefined, at will. Existing parts can be added upon. Ruby tries not to restrict the coder.
For example, addition is performed with the plus (+) operator.
But, if you'd rather use the readable word plus, you could add
such a method to Ruby's builtin Numeric class:
class Numeric
def plus(x)
self.+(x)
end
end
y = 5.plus 6
# y is now 11
Blocks: A Truly Expressive Feature
Ruby's blocks are also seen as a source of great flexibility. A programmer can attach a closure to any method, describing how that method should act. The closure is called a block and has become one of the most popular features for newcomers to Ruby from other imperative languages like PHP or Visual Basic.
search_engines =
%w[Google Yahoo MSN].map do |engine|
"http://www." + engine.downcase + ".com"
end
Ruby and Mixins
Unlike many object-oriented languages, Ruby features single inheritance only, on purpose. But Ruby knows the concept of modules (called Categories in Objective-C). Modules are collections of methods.
Classes can mixin a module and receive all its methods for free. For example,
any class which implements the each method can mixin the
Enumerable module, which adds dozens of methods that use
each for looping.
class MyArray
include Enumerable
end
Ruby's Visual Appearance
While Ruby often uses very limited punctuation and prefers English keywords, some punctuation is used to decorate Ruby. Ruby needs no variable declarations. It uses simple naming conventions to denote the scope of variables:
varcould be a local variable.@varis an instance variable.$varis a global variable.
Beyond the Basics
Ruby has a wealth of other features, among which are the following:
- Ruby has exception handling features, like Java or Python, to make it easy to handle errors.
- Ruby features a true mark-and-sweep garbage collector for all Ruby objects.
- Writing C extensions in Ruby is easier than in Perl or Python, with a very elegant API for calling Ruby from C.
- Ruby can load extension libraries dynamically if an OS allows.
- Ruby features OS independent threading. Thus, for all platforms on which Ruby runs, you also have multithreading.
- Ruby is highly portable: it is developed mostly on GNU/Linux, but works on many types of UNIX, macOS, Windows, DOS, BeOS, OS/2, etc.
References
1 Matz, speaking on the Ruby-Talk mailing list, May 12, 2000.
2 See the Ruby on Rails home page for more.
3 Matz, in An Interview with the Creator of Ruby, Nov. 29th, 2001.
4 Matz, in Blocks and Closures in Ruby, December 22nd, 2003.