Sponsored Link •
Yukihiro Matsumoto, the creator of the Ruby programming language, talks with Bill Venners about morphing interfaces, using mix-ins, and the productivity benefits of being concise in Ruby.
Yukihiro Matsumoto, or "Matz," as he is known online, is the creator of the Ruby programming language. Ruby is an object-oriented language suitable for writing day to day scripts as well as full-scale applications. Matz began work on Ruby back in 1993, because he wanted a language that made him productive while being fun to use. Initially popular in Japan, Ruby has been finding its way into the hearts of programmers all over the world.
On September 24, 2003, Bill Venners met with Yukihiro Matsumoto at the JAOO conference in Aarhus, Denmark. In this interview, which is being published in multiple installments on Artima.com, Yukihiro Matsumoto discusses Ruby's design philosopy, the features of the Ruby language, and becoming a better programmer. In this initial installment, Matz waxes philosophic about design imperfection, the danger of orthogonality, granting freedom with guidance, the principle of least surprise, and the importance of the human in computer endeavors.
Bill Venners: In Ruby, I can add methods and variables to objects at runtime. I have certainly added methods and variables to classes, which become objects at runtime, in other languages. But in Java, for example, once a class is loaded or an object is instantiated, its interface stays the same. Allowing the interface to change at runtime seems a bit scary to me. I'm curious how I would use that feature in Ruby. What's the benefit of being able to add methods at runtime?
Yukihiro Matsumoto: First of all, you don't have to use that feature. The most useful application of dynamic features, such as adding methods to objects, is meta-programming. Such features allow you to create a library that adapts to the environment, but they are not for casual uses.
Bill Venners: What's an example of a library that adapts to the environment?
Yukihiro Matsumoto: One example is a proxy. Instead of designing individual proxy classes for each particular class, in Ruby you can create an all purpose proxy that can wrap any object. The proxy can probe the object inside of it and just morph into the proxy for that object. The proxy can add methods to itself so it has the same interface as the wrapped object, and each of those methods can delegate to the corresponding method in the wrapped object. So an all-purpose proxy, which can be used to wrap any object, is an example of how a library class can adapt to the environment.
Adding methods to objects can also be used in Ruby in situations where Java programmers
use inner classes. For example, if you need to pass a listener object to some method, in Java
you often instantiate an anonymous inner class that defines the listener methods and
pass it. In Ruby, you can just create a plain object—an
instance of class
Object—add the needed listener methods to it
dynamically, and pass it.
Also, because you can replace a method as well as add one, you can use this feature to define callbacks. For
example, every OO GUI library has a
Button object. When someone clicks a button, the
Button's click method is called. In the basic
of course, the implementation of the click method is empty. To add behavior to the button in other languages, you create
a subclass of
Button and override the click method. But in Ruby, if you
want, you can directly replace the click method in class