Over the last couple of years I didn’t pay any attention to the Ruby programming language; you can’t spend your entire life studying programming languages, you have to make choices and get things done.
I worked a lot with Common Lisp and Java and I discovered Ruby when looking at competitive web framework — especially Rails. Ruby has some great advantages that makes me feel comfortable when coming from a Lisp world.
I evaluate a programming language by comparison. I have at disposal two big representatives: Common Lisp — that I consider the most dynamic and innovative programming environment — and Java — the “classic” object-oriented representative. The way you work with both environments is totally different.
Ruby shares more with Common Lisp in terms features and philosophy than I initially thought; much more than aestetic and syntax.
Regarding extensibility, Java use class inheritance or decoration — with the restriction that you can’t extend core classes of the system without forcing the user to using your own definition. Ruby can use the same object-oriented mechanics but can also extend a class by “injecting” new code at runtime:
class Float
def double
self * 2
end
end
2.0.double #=> 4.0
When loading the code above, a new instance method double will be added to the core Float class. This technique is known as monkey patching or “opening a class” in the Ruby world.
Rails make a intensive use of this technique to extend the Ruby environment to be friendly with web development. For example, the Fixnum class is extended with “scaling” methods:
20.kilobytes #=> 20480
20.megabytes #=> 20971520
20.gigabytes #=> 21474836480
20.terabytes #=> 21990232555520
as well as time relative methods:
Time.now #=> Mon Mar 06 21:08:17 CET 2006
20.minutes.ago #=> Mon Mar 06 20:48:22 CET 2006
20.hours.from_now #=> Tue Mar 07 17:08:28 CET 2006
20.months.ago #=> Wed Jul 14 22:08:39 CEST 2004
In Rails 1.1, each object in the environment is extended to support JSON. A new method to_json is made available to serialize any object into a JSON string. Very friendly.
The technique is also used for fixing core classes of the language. As Ruby does not (yet) support UTF-8 Unicode, strings are interpreted as single-byte characters. Fortunately, a standard Unicode library is available to work with multi-byte strings but it comes with a specific API. Rails hackers have come with a module that dynamically extend the base String class to delegate it’s processing to the Unicode library. A change totally transparent for other programs.
Common Lisp programmers are used to metaprogramming for a long time and the above examples can be achieved with generic methods and mix-ins. Ruby make it easy to deal with program extension while keeping coherency and transparency from an user perspective.
It’s one of great aspects of this language.



Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks