![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[livejournal.com profile]](https://www.dreamwidth.org/img/external/lj-userinfo.gif)
Ruby is a nifty little language that's had a small but devoted following for about 15 years. It's gotten a lot of attention recently with Ruby on Rails, a Web development framework that implements the model-view-controller design pattern for Ruby applications. Some things it does very nicely: its syntax is more highly reflective than any other language I can think of offhand since Smalltalk. At some point I should write something about the cooler parts of Ruby, but right now it's annoying me and so I'm afraid that today you get the bile.
There is always the risk, when writing an article like this one, of criticizing something simply because it is different from something that you happen to like more. I am sure this is no exception. I will try not to criticize Ruby for not being Lisp, or C, or heaven help me, Perl, but on the basis of its own merits.
First-class functions
Many methods in Ruby are accompanied by code "blocks" where other languages might use a closure or a reference to a subroutine, e.g.:
values.sort_by { |x| x.abs } palindromes = words.find_all { |w| w == w.reverse }
The confusing and maddening thing here is that you can't pass a function or a reference to one. Why not? Well, because it's not a block, and these methods will only accept a block. What's a block? A block is a chunk of executable code. You mean... like a function? Right. I mean, actually no, not really. What?
Trying to resolve this issue can reduce even the bravest warrior-hackers to a gibbering, cowering mess.
![[livejournal.com profile]](https://www.dreamwidth.org/img/external/lj-userinfo.gif)
Matz confirmed in a Ruby article in InformIT that this "block" construction is a syntactical oddity. It comes into play only when the interpreter sees an open brace (or the do keyword) supplied on the same line as an iterator method. Although it looks as though you are creating an anonymous procedure object and passing it as an argument to the iterator, the block isn't really any kind of first-class object and is not delivered to the iterator via standard argument-passing mechanisms. According to Bendersky, the interpreter quietly transforms the block into a closure and attaches it to a special slot on the iterator method, which the iterator can invoke with the yield statement.
This technique of invoking iterator methods with blocks is convenient but I find it unsatisfying and unsettling when examined more deeply. One of the commenters on Bendersky's essay asked, quite reasonably, why it's necessary to do this:
func = lambda { |x| x + 5 } func.call(2)
rather than the conceptually simpler
func = lambda { |x| x + 5 } func(2)
The answer is apparently that, because Ruby puts methods and local variables in separate namespaces, and permits methods to be called without parentheses, an expression like "func 2" would have an ambiguous parse tree. So you're not allowed to do it.
Alan Perlis once famously said that "syntactic sugar causes cancer of the semicolon." At long, long last, I think I finally understand what he meant.
Inconsistent variable scoping
Ruby variables are declared only in the arguments to Procs or methods. When a new free variable is instantiated, it is assigned to the innermost closure active at run time. Example:
def foo result = 0 (0..5).each do |n| result = n end end
Here exactly one result variable is instantiated across the entire method, and after the each loop terminates, its value is 5. However, in this example:
def foo (0..5).each do |n| result = n end end
the scope of the result variable is limited to the each block, because --- and this is important --- Ruby converts this block to a closure when the each method is invoked (see above). That is important because it explains why this other, semantically equivalent, code behaves differently:
def foo n = 0 while n < 5 do n = n + 1 result = n end end
See the difference? Because the block in the while loop isn't a closure, the result variable here belongs to the scope of the foo method. It is like the first example, except that in that case we had to force the variable's scope by deliberately instantiating it at the method level. That's not necessary here because the while block isn't a closure; only the each block is.
If you are anything like me, at this point you have already begun drinking heavily. Byzantine scoping rules like these are just a recipe for disaster.
Matz deserves a lot of credit for recognizing how bad this is. I have not had a chance to review what's happening with Ruby2, but the changes Matz describes in these slides look like a considerable improvement from the current state of affairs.
Anyway
Despite all this I can't really say that I dislike Ruby. I've been working in Ruby on Rails for only a few months, and have not really delved deeply into what the language has to offer, but I find a lot of its self-redefinition features really encouraging and intriguing. I can see that Matz has tried hard to strike a good balance between conceptual elegance and programming expressiveness, and that's a difficult line to walk. What makes me unhappy is where the language appears to violate the principle of least surprise; if issues like these can be remedied effectively, it will make Ruby substantially more appealing to hackers like me.
no subject
Date: 2008-03-10 12:14 pm (UTC)I don't know Ruby even a tiny bit, so I can't comment on most of your post, but this criticism caught my eye.
Is the ability to pass a function/reference to a function a desirable trait?
I always thought of that as one of those arcane things that C and C-like languages let you do to shoot yourself in the foot and several other body parts...and/or to ensure you have a job forever because that critical piece of code you wrote is completely unintelligible to anyone but you, and if they fire you the entire company will go up in flames of spaghetti.
Highly structured languages like Pascal certainly don't let you pass function references. Nor highly OO languages like Java. (Although of course they do let you pass an object and therefore refer to the object's methods.)
no subject
Date: 2008-03-10 12:15 pm (UTC)no subject
Date: 2008-03-10 01:03 pm (UTC)Oh golly yes. For a functional language it's really a requirement. It lets you do things like:
When your language makes a function a first-class object (a quantity that you can assign to variables or store in data structures the same way you would any other quantity like a string or an integer), this kind of idiom quickly becomes very natural. I'm not sure I'd say that Ruby is "a functional language" but it certainly inherits enough functional traits (like functions as first-class objects and a "lambda" function which is used to create them) to feel like one.
You're right that doing this in C is a good way to make your application blow up, but that's partly because the way you implement this in C is with pointers to functions, which gives the compiler no good way to check the type or number of arguments you're passing to your dereferenced functions, so it kind of has to say "I hope in hell you know what you're doing", close its eyes and jump. A decent dynamically typed language doesn't suffer from those shortcomings and makes it a lot easier to build code like this.
no subject
Date: 2008-03-10 01:07 pm (UTC)The last functional (which I assume you mean in contrast to object oriented) language I used was C, in which this would be a total nightmare. (Reason #4591 to avoid C, eh?) And in Java and other OO languages, this is a non-issue...you'd just operate on your object using whatever methods it provided to do so.
So...why DOESN'T Ruby allow it?
no subject
Date: 2008-03-10 01:50 pm (UTC)Well, no, what I mean by "functional language" is, well, a functional programming language (http://en.wikipedia.org/wiki/Functional_programming). Languages like C and Pascal are usually described as "procedural" or "imperative" (http://en.wikipedia.org/wiki/Imperative_programming) languages. The world is not divided into "Java" and "everything else," much though Sun may want us to think so. :-)
And in Java and other OO languages, this is a non-issue...you'd just operate on your object using whatever methods it provided to do so.
I'm not sure I get that it's a non-issue. My ignorance of Java is showing here, but it seems as if Java 2's Array sort method (http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(java.lang.Object[],%20java.util.Comparator)) can take a "Comparator" as an argument, which looks like how you might implement the example I gave here. It's not clear to me exactly what a Comparator is (the docs seem to say that it's an "interface"), but the point is that the object itself doesn't necessarily know how you might want to sort it at any given moment; you have to provide a different thing (the comparator) that has that knowledge.
So...why DOESN'T Ruby allow it?
Didn't you read my post? :-) Doing so would interfere with Ruby's syntactic sugar that allows you to call methods without parentheses.
no subject
Date: 2008-03-10 02:17 pm (UTC)The reason I said it is a non-issue is because there are other more typical ways to handle this. Not that you *can't* have a class that exists solely as a reference point to methods, but that more often you would see a class that defines a method so that the object knows how to compare itself to something else. (ie: the object would implement the Comparable interface (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html)).
no subject
Date: 2008-03-10 04:28 pm (UTC)You're certainly right, there are other ways to do it. But this lets you handle arbitrary stuff.
Where C passes function pointers, Java passes objects implementing an expected interface, which may or may not have been defined on the fly. Java anonymous classes aren't quite as good as Lisp closures, mostly because they can only close over "final" variables, but they add a measure of convenience (and reduce, for once, the amount of code you have to type to get that functionality).
no subject
Date: 2008-03-10 04:31 pm (UTC)I always thought of that as one of those arcane things that C and C-like languages let you do to shoot yourself in the foot and several other body parts...and/or to ensure you have a job forever because that critical piece of code you wrote is completely unintelligible to anyone but you, and if they fire you the entire company will go up in flames of spaghetti.
I responded to this a bit below already, but Java OO code uses interfaces for (mostly) the same reasons that C would let you pass function pointers. There are situations where it's better to figure out what the right thing to do is during run time, not at compile time.
no subject
Date: 2008-03-10 05:57 pm (UTC)no subject
Date: 2008-03-10 06:17 pm (UTC)Of course I haven't used Pascal in...erm...22 years.
Amen
Date: 2008-03-10 01:38 pm (UTC)And dbang: C is not a functional language, it's a procedural language. When qwrrty says "functional" he means this (http://en.wikipedia.org/wiki/Functional_language).
no subject
Date: 2008-03-10 02:20 pm (UTC)I know nothing about it personally, but I met a guy recently who was waxing evangelistic about it rather vehemently, saying that he had been a big Ruby on Rails fan, but had totally converted to Groovy, saying it solved all Ruby's flaws. I had nothing to add to the conversation, of course, knowing neither Ruby nor Groovy, but it did sound, um, groovy.
no subject
Date: 2008-03-10 02:53 pm (UTC)no subject
Date: 2008-03-10 03:19 pm (UTC)no subject
Date: 2012-05-31 03:09 pm (UTC)I've seen it used to incredible effect in Java shops for writing tests, adding a scripting framework, and generally offering a more dynamically typed, less verbose programming experience than pure Java provides.
no subject
Date: 2008-03-10 05:04 pm (UTC)One thing that bugs me is that there is no convenient way to iterate through two or more collections at once. I had a problem where I had two lists of numbers, one in a file and one in a database table. I had to generate a pairs from one number from each list. This was just test data, so I didn't need to care what order it worked in or how they were paired, and randomly would have been the best way. Given how easy it is to iterate through one collection, I was bummed when this task actually slowed me down. I ended up with a SQL hack to do the pairing in the database.
no subject
Date: 2008-03-16 11:42 pm (UTC)def pairs( seq1, seq2 )
loop do
yield seq1.next, seq2.next
end
end
lst1 = 9.downto(1)
lst2 = 20.downto(12)
pairs( lst1, lst2 ) { |x,y| print "#{x},#{y}\n" }
EDIT: I was still curious, so I installed MacRuby which is a 1.9 port. Seems to work as expected.
no subject
Date: 2008-03-17 12:03 am (UTC)no subject
Date: 2012-05-31 03:11 pm (UTC)Thank you!