Ruby or not Ruby

Once I knew I wanted to study CS–that was late in 2014, so roughly three quarters of a year before the start of the winter term 2015–I realized it would be a good idea to learn how to program. Other than HTML/CSS I had never written an actual line of code in my life.

Fortunately I stumbled upon one of the best tutorials, in my opinion, on the internet: Introduction to Computer Science using Java by Bradley Kjell. It’s a bit long in the tooth to be sure, but unlike most “tutorials” on the internet this is not just a thinly veiled brief reference to a few basic language features, but an extensive, full-blown programming 101 in over 80 chapters, starting at first principles and moving on to details like file I/O, both text and binary, and GUIs. I worked through it very thoroughly over a few months, doing all the exercises (there are between 3 and 10 after each chapter, some quite involved), and had fun. Then I felt ready for more.

I discovered that my old interest in languages and their peculiarities easily extended from natural to programming languages. I felt a sudden urge to not stick with Java–much as I thought I loved the language, it having become sort of my computer mother tongue–but learn a few other languages. Preferably as different from Java and from one another as possible.

For a while I amused myself by reading all those eternal “which programming language should I learn” threads on the internet, studying pro’s and con’s, charts and comparisons, finding the most highly recommended books for individual languages, and so on. The possibilities seemed limitless: Languages recommended for being easy to learn (Python), for being way cool (Ruby), for being dead ugly, but much sought after (JavaScript), and for being sort of the Greek (C) and Latin (C++) of modern computer programming: the ones you had to study to understand all the other ones based on them, even if you’d never actually use them.

Tough choice.

I felt most attracted to Ruby. It had that sophisticated and somewhat arcane touch. And there was a much praised book, The Well-Grounded Rubyist by David A. Black, which looked like it could be more than just a language reference–as a former academic, I still love a book that you can actually read. So I got me the book and read it in the evenings instead of novels, for a while. I enjoyed it, too. After I had finished it, I moved to Eloquent Ruby by Russ Olsen, and loved that one too.

Only you don’t learn a language this way. You have to write it, and I never had written Ruby. So I started doing the Ruby exercises at exercism.io, got feedback and looked at other solutions for the little puzzles that were sometimes quite tricky, and discovered what a cool language Ruby really was. Very high-level, extremely expressive, often saying in half a line what other languages needed 8 or 10 lines for. And highly idiomatic, too, to the extent that you seemed to prove yourself as being a member of a secret, conspirational, know-the-handshake society by using certain expressions and shying away from others. I suppose you can love that or hate that. I do neither–I find it slightly amusing–, but I have come to love Ruby itself.

For a while I worked through a second Java book to get a different perspective: Objects First with Java, by David J. Barnes & Michael Kölling, except I listened to my wallet and read it in the more reasonably priced German translation, something I almost never do, but in this case the cost of the original American edition was quite absurd. That book too is full of exercises, focusing not on language features, but on object-oriented design. That was useful, though a bit boring. What I took away was probably primarily a feeling for how weird using German class and variable names together with English language keywords is.

Then I got really really curious about all those claims that you really didn’t know anything about computer programming unless you knew what pointers were, those mysterious and apparently very scary things that always cropped up in practically every discussion about that old jack-of-all-trades language, C++. From many suggestions I picked the introductory book by the language designer himself, Bjarne Stroustrup’s Programming: Principles and Practice Using C++, in the 2nd (2014) edition.

Bad mistake. An extremely chaotic book, always oscillating between explaining the most basic features and leaving the reader alone with trying to understand unnecessarily (I hoped) complex pages of code that often involved concepts introduced much later in the book, or not at all. I again took it very serious, doing almost all the exercises (10 to 20 for each chapter), and had some fun until about chapter 10. I suffered through the GUI chapters in spite of the hopelessly messed-up interface the author provided for FLTK, a popular graphics tool kit, which I had much rather used directly and sometimes did. I lasted through chapters 18 to 20 which finally introduced pointers, and then gave up, realizing that my interest in C++ originally had been nothing but curiosity, and that I was already way too deep into a language that I had come to hate with a vengeance. But I can now confidently say I know what pointers are, and why people loathe them so much …

Then I switched once more to an entirely different language and worked through most of the online version of Eloquent Javascript by Marijn Haverbeke while watching some of the YouTube videos by Douglas Crockford (the guy who thinks he has found the good parts in JS, though they aren’t very numerous) and also studying JavaScript: The Definitive Guide by David Flanagan. I can’t say I managed to really love JavaScript, though I did see some of the points, above all that it’s a highly functional language, but come on–no classes? Besides, it was now late in the summer of 2015 and I was running out of time.

So after about 9 months of self-study I came to CS at UAS with not so much a thorough basis in programming in a particular language, but with some idea of key concepts (I had read quite a few programming classics on the side) and a certain feeling for what programming languages had in common and where they differed. And a keen interest to learn more.

I was thrilled to learn that the language for Programming 101 would be Ruby, my secret love as programming languages go. If nothing else this had the advantage that for the first couple of months there was very little new I could learn in the programming courses so I could concentrate on other things.

After a while, however, I realized that nobody had any intention of actually teaching us proper, idiomatic Ruby. In fact it’s being actively discouraged to learn the kinks and quirks of the language because this will spoil us for the “real”, serious language we’ll be learning from the second term onwards, i.e Java. Ruby is regarded–and taught–not as a language in its own right, but as an ‘easy’ introductory language that saves you some of the unpleasentnesses of writing a strongly typed language and has convenient shortcuts for common operations like file I/O–as a sort of ‘Java light.’

Of course that’s often a real injustice to Ruby. And it makes for some real ugly code that’s neither proper Ruby nor proper Java. For instance we are being taught to write while statements and for-loops. But that’s not how it’s being done in Ruby. One of the core concepts of Ruby is a large number of specialized iterators that save you exactly all those loops. In Java you would have to write 8 to 10 lines of code for a simple factorial function. In Ruby half a line does the trick:

(1..n).reduce(:*)

Yet this is being regarded as sort of Ruby peculiarity that has no relevance in real programming. What this suggests is that there is a generic, universal programming style that’s independent from which language you write in. I don’t think that’s true. Every language has its own grammar and its own style, and it should be taught properly or not at all, but not as a soft version of another language. Yes, you can write Ruby that looks and works roughly like Java without variable typing, but that’s not fair to either language.

The slides for our Programming 101 course are actually written for Java, then superficially translated into Ruby, and it’s showing. They use a lot a parentheses. Ruby doesn’t. They enclose multi-line blocks in curly brackets. Ruby uses ‘do … end’. They have tons of explicit return statements complete with the ‘return’ keyword where Ruby implicitly returns the value of the last statement of a method. In fact, in places they still sometimes say ‘Java’ instead of ‘Ruby’ as if somebody messed up the search/replace. Sometimes our professor qualifies a claim by saying something like “well, I’m not sure if it’s so in Ruby, but it is in Java.”

A further symptom of this learning-Java-via-Ruby concept is that we are being urged to use Eclipse as an IDE for Ruby. Now Eclipse is a great tool for writing Java, but writing Ruby in Eclipse is a real pain. The Dynamic Languages Toolkit (DLTK) plugin has a very weird sense of Ruby syntax. Sometimes real errors are not identified, and often correct Ruby statements, particularly as spaces and line breaks are concerned, are erroneously marked as syntax errors. Much worse, there is no refactoring support at all. It’s not bad, it’s simply not there. Not even something as basic as renaming of variables. I am not even dreaming of automatic extraction of functions, updating of ‘require’ statements when class names change, and so on. In short, Ruby in Eclipse is worse than writing Ruby in a simple text editor. Why aren’t we being encouraged to use a proper editor? For me it’s Emacs all the way. With a few plug-ins it supports Ruby perfectly.

Then of course in a couple of months it will hardly matter. In the summer term we’ll move from Ruby to Java, and finally at least using Eclipse will make sense. And I’ll no longer have to sigh inwardly over my favourite language being abused.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s