Introduction
Ruby is the new boy in town. Until a couple of years ago, nobody really
spoke about Ruby. It was really popular in Japan, but outside it was just
one of the many ’scripting’ languages that the open source world has
developed over the years.
The big names were using Python or Perl. Well, most of them were using PHP,
but in fact this does not matter: while Perl, Python and Ruby are excellent
languages (even if it is almost impossible someone really likes both Perl
and Python, nonetheless Perl has its merits), PHP plainly sucks.
And then of course
there was that Java everybody was talking about and C# (but the open source
guys usually said positive things about it only if in the same sentence
the word ‘mono’ occurred).
In the last few years Rails changed things. Now a lot of people are talking
about Ruby and dynamic languages. A lot of Java developers discovered the
power of dynamic languages and dynamic typing, after years spent saying
that the only saviour was static typing. In fact it seems to me that there
is kind of a revolution in the IT world.
Python and Ruby and Lisp and …
Rails is a pretty good framework. The documentation is acceptable even for
those who grew up with Sun’s bloated documentation (I still prefer to read
the code if the language itself is readable enough).
Ruby is a really nice language. In fact you can see it as a (Perl–)++.
It kept almost all nice features from Perl, and dropped some of the worse.
Of course a Perl Monger would disagree, but I’m a pythonist after all.
One of the most interesting things, in my opinion, is that Ruby and Python
are quite similar (even if they start from different points of view). I feel
quite comfortable with both languages, and even their pragmatic is not
really different. Moreover differences tend to depend on different
syntax, rather than on different semantics.
For example in Ruby is quite frequent to ‘open’ classes and add methods.
This is something you can do in Python, even if there isn’t a special
syntax.
>> class A(object): pass >> a = A() >> a.foo() Traceback (most recent call last) ... <type 'exceptions.AttributeError'>: 'A' object has no attribute 'foo' >> def ext_foo(self): print 'foo' >> A.foo = ext_foo >> a.foo() foo
Of course this is far less elegant, and that’s way it’s something less
frequently done in Python.
Ruby has another ‘advantage’ over Python. It’s syntax is less rigid,
and in fact the most typical thing that is in Ruby pragmatic is to
write a DSL in the language itself and program in that DSL.
There are a lot of examples of this strategy in Rails itself. However, this
approach is not new at all. Lisp programmers have been using it since the
beginning, that is to say it’s almost forty years they code DLS in Lisp and
use them to write the application.
Duck typing vs. duck hyping.
Unfortunately I hear people saying quite often that ‘Ruby invented duck typing’. People that just
discovered Ruby, seem quite certain about it. Some of them even told me that
in Python you ‘were not really duck typing’ and that it was something
peculiar to Ruby. But let’s look at the definition (from Wikipedia):
Duck typing is a form of dynamic typing in which a variable’s value itself implicitly determines what the variable can do. This implies that an object is interchangeable with any other object that implements the same interface, regardless of whether the objects have a related inheritance hierarchy. Duck typing is a feature of programming languages such as Smalltalk, Python, Ruby, and ColdFusion.
Of course this is something that you can do in Python. And you can also do it in Objective C, by the way. And in Python it is something plainly obvious, too. Everybody does it, everybody likes it. But nobody thought about using a fancy name when describing the practice in books and blogs.
Amazingly, the term ‘duck typing’ (that some think it’s a Ruby peculiar feature) was probably used for the first time by Alex Martelli when speaking about Python typing system. It was Jul 26 2000, and the english language Ruby newsgroup had been active since the first days of may. There was a mailing list of course, but nonetheless Ruby was relatively unknown outside Japan.
In other words, don’t check whether it IS-a duck: check
whether it QUACKS-like-a duck, WALKS-like-a duck,
etc, etc, depending on exactly what subset of duck-like
behaviour you need to play your language-games with. If
the argument fails this specific-ducklyhood-subset-test, then
you can shrug, ask “why a duck?” (at least, you can if you’re
a Marx Brothers fan and have memorized “Cocoanuts”‘ script;
Monty Python one-true-wayists will have to find their own
simile here), and move on to the next set of tests (why-a-no-
chicken immediately comes to mind, but then one would have
to ask why it crosses the road, so I think we’d better snip it).
That is the reason why I say that Ruby supports ‘duck hyping’. Ruby is a very good language. I suppose that if I learnt Ruby before Python, I’d probably like Ruby more than Python (because the languages are quite equivalent, it’s my head that now works in a somewhat more pythonic way). However, there are a lot of people that are really *good* at selling Ruby. An excellent publisher based his early success on Ruby books, the creator of Rails has lots of marketing skills, and so on.
However, I was wondering: why Ruby and not Python? I don’t believe in fortune. To discover the answer (that in this particular case I guarantee it’ s not 42) we must analyze in which ways syntactical features of Ruby (since semantically they are quite interchangeable) influence the language pragmatic, and in which ways the pragmatic contributed to build Ruby success.
And when we are into pragmatic, we are not in the world of the compiler: we are in the world of the programmers. That is to say: we must understand why Ruby has appeal even where Python has not. We must understand in which way the language influenced the way of thinking of so many people, and why Python did not.
4 comments
Comments feed for this article
March 13th, 2007 at 17:59
riffraff
good post, and I 99% agree (even if I _do_ like both perl and python :).
Where I disagree is in that python didn’t influence the way people are thinking.
In my opinion, python did it, it’s just that now we take this new mindset for granted.
As for the duck typing thing.. I remember that this exact expression was attributed to dave thomas, but coined offline, the “if it walks like a duck it is a duck” thing is old english saying.
But otoh Dave knew python and Smalltalk before ruby, afaik..
March 14th, 2007 at 05:59
Enrico Franchi
Thank you. In fact I used Perl *before* Python.
However, I just agree with most Python principles (that is to say I disagree with the corresponding Perl ones). That means that Perl pragmatic and philosophy does not appeal me.
Regarding ‘duck typing’ I suppose Alex was the first one to use the metaphor to describe the Python typing habits. But everybody knows about Dave, nobody about Alex.
About the mindset: in fact Python has been around for quite a lot of time. And it’s popularity grew constantly (and is still growing). Of course it changed the minds of those who used it. It influenced Ruby itself, other languages and programmers that are not strictly part of the Python community.
However, it was not well known outside the ‘unix open source community’. The popularity among programmers is quite good, but in the IT world it is not well known. I mean, in my country there has been only *one* translated Python book for years.
Moreover a lot of people who knew something about Python were likely to criticize the language because it is dynamically typed. And ‘true languages’ are statically typed. And because it is a ’scripting language’. And because ‘Java is the Way’.
Now a lot of the same people are interested in Ruby. This is something that I feel quite strange. I think it’s unlikely that you dislike Python telling it’s a ’scripting language’ and you like Ruby. That is the point of my question.
March 14th, 2007 at 06:46
el raichu
well, i have to disagree on the point that the difference is not just in syntax. in my opinion, the adoption of a programming language is more of a sociological phenomenon than an engineering one.
for instance, take the issue of python’s anonymous functions (http://www.artima.com/forums/flat.jsp?forum=106&thread=147358). python’s bdfl considers the multi-line lambda unpythonic. now, unpythonic is not a programming issue; plenty of programmers all over the world use multi-line lambdas all the time. instead, being “unpythonic” is penalized socially; the python community discourages such steps in the language because they somehow feel that the simplicity of their language is more important than the multi-line lambda.
or take the social problems of lisp (http://c2.com/cgi/wiki?SocialProblemsOfLisp). the lisp community appears unapproachable, especially when the supposed “elders” of the community, who should be taking care of the newbies, give them trojans instead (http://social-problems-of-lisp.blogspot.com/2006/06/pascal-bourguignon-come-on-down.html).
why are people drawn to ruby? i bet that, for many people, it’s not the language, it’s the community. how many languages do you know have guys on board with weird names like why the lucky stiff (all praise the chunky bacon) (http://whytheluckystiff.net)? the wackiness is definitely part of the appeal; the duck typing less so.
March 14th, 2007 at 14:55
Enrico Franchi
I suppose I haven’t been clear enough. In fact we are kind of saying similar things (unless I didn’t really get what you meant).
I hope we agree that ’semantically’ Python and Ruby are quite similar. They have a strong syntactical difference (Python is much more disciplined, and some operations have a neater syntax in Ruby — or vice versa). I was wondering how this syntactical differences influenced the pragmatic (that is to say the way we use the language).
My example is that although both languages allow to add methods dynamically to classes and to objects, this is done *very* often in Ruby (and Ruby has a neat syntax to do it), while in Python it is seldom done.
And I agree in what you say about the community. The Ruby community is great and is very good at marketing Ruby. I mean, everybody knew that MacOS X Leopard would include Rails and RubyCocoa. However, almost nobody knew it also includes PyObjC and Twisted (that is used in a new Apple application). (of course these are rumors — as far as I know –).
Names are of course a good way to sell things. And guys like Hansson are perfect to create hype. For example picture like [http://www.flickr.com/photos/eugevon/130610241/ this]… I mean here he’s not talking like a techie, but rather like a cool director or something like that.
However, I suppose there must be *more* than community. Otherwise it means that you don’t really need to create a good language with good libraries. Tomorrow I don’t want to be using a language because something got the idea to pretend some particularly sexy chicks created a very cool language…