Text in English Thinking aloud about a Ruby book

After Labra's enthusiastic recommendations about Ruby, and although I usually don't feel too excited with new programming languages whose main contribution I can't tell, I've just started to read some introductory texts: Programming Ruby - The Pragmatic Programmer's Guide, by Dave Thomas and Andy Hunt. And as I read (I had no previous knowledge about Ruby) some thoughts came up.

First, the authors say:

Ruby syntax is clean. You don't need semicolons at the ends of statements as long as you put each statement on a separate line.

What? Do semicolons make the difference between clean or unreadable code? Is line-based coding always better? Mmmmm, it seems that we have a religious war on the horizon.

Ruby doesn't use braces to delimit the bodies of compound statements and definitions. Instead, you simply finish the body with the keyword end.

Once again, the claim that using an ending keyword can be described as "simply" and the use of brackets cannot is a bit subjective and unfounded, I think.

Why does one call have its arguments in parentheses while the other doesn't? In this case it's purely a matter of taste. [...] However, life isn't always that simple, and precedence rules can make it difficult to know which argument goes with which method invocation, so we recommend using parentheses in all but the simplest cases.

I wonder: what kind of benefit can bring having loose rules about calling syntax? In addition, what'ts the point of building such flexible rules when one has to admit that using parentheses is recommended anyway? And if we omit parentheses only in tne simplest cases, wouldn't the different, unpredictable, arbitrary use of parentheses eliminate the supposed benefit of being "free" to use them or not?

Of course, this is not relevant regarding neither the book nor the language itself. I think it's just an arguable start. But if Labra likes Ruby, it must be worth it. I'll keep reading.

2 comentarios:

Mafias dijo...

Well, as a reader of your blog, and being a Ruby enthusiast (the only one I know outside the net, Ruby is not a very popular language in Spain yet), it was predictable that I was going to comment :-P

Programming Ruby (more known as The Pickaxe, because of the illustration in the cover) is a great book, but maybe not the _best_ book for the beginners. Many times you feel like the authors are trying to convince you to buy the book, or start programming ruby. "Hey, I am already reading the book, give me a break!" :-)

About your thoughts...

The semicolons are a religious war. There's no sense in arguing with that. However, you can use semicolons for separating statements if you want, even if it's not a very popular choice.

The use of braces or begin..end (a la pascal) is another religious war. I like braces more than "ends", but after a few months programming in ruby I admit that the ends are cleaner in nested loops and other stuff. Again, in blocks, closures, Procs... you can use braces if you want (they have different precedence than do..end, but in almost all cases using dos or braces doesn't change anything )

The parentheses. Here we've got an issue, and this isn't a religious war anymore. In fact, I think that the optional parentheses are one of the best ideas in Ruby (borrowed from Smalltalk, if I'm not wrong). Why could be a good idea that loose syntactic rules? Because of the "magic behind". In Ruby _all_ is an object, and I mean ALL; all operations are methods, even the aritmethic operators. But the optional parentheses can create an illusion and make you believe you're using reserved keywords, or creating new ones. Look at "public" or "attr". They're methods of the Kernel class, but you can change the visibility of the code (as in C++) with the first, and add quick properties with the second. And you don't have to worry if you're using a feature of the language, or if you are calling a method. You simply don't care, and things work. It seems almost an heresy coming from C-like languages, but is fantastic when you get used. In general, using parentheses in your code is a good idea, but sometimes they aren't necessary. Come on, we're old enough, we don't need to put parentheses on a method without parameters... Statements like
sorted_ array = [3,4,1,2].sort!
seems more elegant to me that an equivalent in Java
int[] arr = {3,4,1,2};

Matter of taste, anyway. We'll keep talking about Ruby if you keep the interest on it ;-)

Sorry for my awful english, anyway. I wrote in English to practice a little and hoping I'd write less than in spanish, but my comments always are eternal to read... Sorry for the brick, que dirían los Gomaespuma :P

Guti dijo...

Very interesting opinions, thank you for sharing them!

Actually, I did think that there were probably some compelling reasons behind Ruby's syntax. So the (small, anyway) guilt goes to the authors. They should have made your clarifications in advance. When speaking about semicolons, they should have anticipated that "this is probably a religious issue, but Ruby programmers in general feel comfortable without them", or something like that. Regarding the parentheses, I think they could have said at least something like "There are reasons for this that we'll address later, when speaking about objects". And so on. The claims they make, just as they make them, have exactly the effect you described: "Ruby is great, buy it, buy it".

Your response is not any brick; one needs some space to explain things, and you've explained some of them that the book did not address properly. So thanks a lot for the brick :-)

As for your english, as far as I can tell (not being a native speaker myself), I find it excellent!