[OT] Ruby (was Re: Spreading Smalltalk)

Avi Bryant avi at beta4.com
Wed Apr 17 05:14:57 UTC 2002


On Wed, 17 Apr 2002, Bob Ingria wrote:

> Actually, as someone who's hacked both LISP and Smalltalk, Ruby feels more
> LISP-like than Smalltalk-like to me (e.g. things like mixins, and various
> other language features).

Yeah, I've talked to Matz about this, and he was a LISP hacker, not a
Smalltalker, before developing Ruby.  There are certainly a few ways in
which Ruby has the feel of a LISP - for example, it's usual not to use
explicit returns, but to rely on everything being an expression; it's
file-based, and evaluation-order based, and there are a lot of
meta-programming games that come from that (unfortunately using eval
instead of real macros).  Interactive programming can be done at an REPL
loop.  There's a lambda method, although it's rarely used.

However, even if he'd never hacked Smalltalk, Matz was obviously heavily
influenced by it.  The object model is nearly identical to Smalltalk's (in
terms of the way the Class/Metaclass system works), there's an equivalent
of DNU (method_missing), the collection protocol is mimicked
(collect/select/detect/reject, although missing the more interesting
variants like detect:ifNone:), blocks are used idiomatically in much the
same way that they are in Smalltalk, adding methods to system classes
is encouraged, etc.

Since I often see the programming language world (at least, the small
enlightened part of it) as being divided into the LISP camp and the
Smalltalk camp, it's nice to see something like Ruby strike a decent
balance between the two.  My particular style of writing Ruby is an
interesting example of this: Ruby has the peculiarity that code inside a
class definition, but outside a method definition, is evaluated in the
context of the class object.  In other words, this code

class Foo
  def some_method(x)
    x * 2
  end

  print self
end

will print "Foo".  This means that it is possible to write class methods
that use eval to act more or less like macros, but macros that can be
overridden by subclasses, etc.  The classic example of this, since it's a
standard part of Ruby, is the method Class>>attr_accessor.  This is used
like so

class Foo
  attr_accessor(:bar)
end

and generates a getter and setter method for the 'bar' inst var within
Foo.  The meta-inclined can make very good use of this style: there's a
Ruby implementation of the X11 protocol, for example, which reads more
like a specification than an implementation, the bulk of the code being
generated by declarative calls to class methods.

I've played with doing similar kinds of metaprogramming in Squeak, but
I find that image-based development doesn't lend itself naturally to such
idioms.

Cheers,
Avi




More information about the Squeak-dev mailing list