Skip to content

Seven Languages in Seven Weeks – Week 1 – Ruby

Sat 19th January 2013

Day 1

Monday 7th January 2013

Ruby is apparently like Mary Poppins. That’s fine, I quite like Mary Poppins šŸ™‚

Spent someall of the evening setting up a Ubuntu Desktop VM, for experimenting with these languages. 12.10 struggles to run at all in VirtualBox; turning on 3D acceleration helps to make it a little less sluggish. Then installing Guest Additions seemed to break it. Let’s try 12.04! After that failed to do an apt-get upgrade, I was beginning to get angry. Eventually got it and ruby 1.9.3 installed, and then did Hello World. Running a bit behind…

Tuesday 8th January 2013

Read the rest of Day 1, and did some of the very basic exercises. Unfortunately, the Ubuntu 12.04 VM is so slow, I couldn’t run a web browser in it. I’m going to see if I can get away with SSHing to a server version of Ubuntu; I think most of these exercises are going to be on the command line.

The book introduced some of the syntax. Seems a bit similar to Python so far. Not quite as forgiving about type as, say, PHP (you seem to need to convert/cast explicitly more often, for example .to_s and .to_i). I really like the ability to put the if and while clause at the end of a statement, so it reads like a person would actually say it. I also love the fact there is an unless clause, which lets you do if not in a clearer way šŸ™‚

The exercises were extremely simple, encouraging me to look at the API and search for some common methods. The final exercise was a guessing game, which I’ve used as an early exercise in some programming courses: it’s clearly a classic!

It’s a bit odd that Ruby has some clever loop syntax, but does not have a do .. while loop. Or does it?

Day 2

Wednesday 9th January

Set up 2nd network adapter so I can SSH to the VM.

Code blocks are quite cool, although the yield keyword to pass control over to the code block is a bit weird.

I really like how you can add to any class anywhere; I just did it in interactive mode, by adding a method to the Fixnum (numeric literals) class!

Not sure I like the propensity to name everything with underscores, though maybe that’s because I’m typing one handed while reading the book!

Friday 11th January

Learned about classes today. Reasonably standard stuff to begin with. The way of automatically creating accessor methods is interesting, though the syntax seems a little unintuitive:

attr_accessor :name

The mixin approach to inheritance is also quite nice, but the book doesn’t really go into any detail about how it handles method name clashes, which is what I thought the main problem with multiple inheritance was (even though the book seems to imply that the mixin approach is better than multiple inheritance).

Including comparable allows you to use a whole bunch of existing methods on enumerables (such as the collections array and hash). enumerable has to implement the each method and comparable needs to implement <=> (which has the extremely cool name “the spaceship operator”!!) However, I’d quite like to know how Ruby forces you to implement these: is it explicit like in Java, with an abstract keyword or some similar syntax, or do you just get runtime errors if you don’t implement them? The book doesn’t explain!

Trying to work out how to “access” files for one of the exercises. Unfortunately, the Ruby API for the File class says that open returns a file object, but there is no information about the methods that exist within it! Where do I find out about the read and write methods?! Oh I see, I have to find the superclass of File, which is IO, and the methods are in there. Why aren’t they referenced in the body of the documentation of File somewhere?

Special variables, for things like input record separator and line number, are quite nice, a bit like AWK and Perl.

Day 3

Tuesday 15th January

The has_many and has_one keywords are intriguing. The book says they add the instance variables and methods to handle those relationships, but doesn’t really explain what that means. I guess it means it adds a single instance variable for has_one, and adds a collection of them for has_many?

Being able to add to any class at any point is pretty neat, a bit similar to JavaScript’s ability to update the prototype of an object. And I think Python allows it?Ā And maybe Perl? Maybe every interpreted language with object orientation allows you to modify any class anywhere in code? Although I’ve not really seen it done in PHP (turns out it’s not allowed).

The book has a simple example of these Domain Specific Languages I’ve heard so much about, but as yet don’t fully understand. However, the examples do not quite demonstrate the usefulness of being able to write 10.miles.forward and have it converted into inches (a bit of thought suggests that it’s useful for calculations, but you’d have to be reasonably explicit in the documentation about how to use this DSL).

method_missing is cool, and much like the __call magic method in PHP. Being able to handle methods in such a dynamic way (without having to write every single combination, essentially allowing the method name to contain variable elements) is quite fun šŸ™‚

I have deduced from the API of gsub!, that the exclamation mark is probably a style standard to differentiate between methods that return a new copy of the object and ones that update in place, essentially allowing objects to be both mutable and immutable depending on how the application developer wants to use it, as opposed to being enforced by the creator of the language or API.

Friday 18th January

acts_as_csv_module.rb:20: unterminated string meets end of file
acts_as_csv_module.rb:20: syntax error, unexpected $end, expecting keyword_end

When in fact, the file is 38 lines long (so the unterminated string has not met the end of file on line 20), and the unterminated string was on line 15 (so saying that in the error would have been more useful)!

This section has 3 examples of doing the same thing in 3 ways, but it’s not that clear what the improvement is. I searched to find out a bit more about define_method and base.extend.

The CSV example is a really terrible example of metaprogramming, because it doesn’t do any metaprogramming. If it supported accessor methods based on the column headers in the CSV, I would understand, but it doesn’t (turns out that’s the exercise I have to do next)! And there’s absolutely no explanation why the last example adds a class method called acts_as_csv, which itself just adds some instance methods, then calls it immediately upon initialisation.

acts_as_csv_row.rb:50: stack level too deep (SystemStackError)

Actually, the error was that the local variable hadn’t been declared (should have been using the instance variable).

While I learned a little about Ruby by doing that exercise, I mostly learned that my CSV file did not comply with the book’s assumptions, and that Ruby’s error messages and behaviour of things like puts are not helpful, or at least not when you don’t know the language!

Conclusion

About Ruby

The style of Ruby irks me a little. The underscores and contractions in method names (e.g. to_s, to_f), the * to (sort of) denote variable arguments and & for passing code blocks, feels a bit like the hacky bits of C but deceptively so, as it doesn’t really function like C at all. I’ve also never been much of a fan of languages that use def and end to wrap their functions: it seems neither bluntly obvious as the keyword function (JavaScript) nor as concise as the C-style function definitions.

No definitive API documentation seems to appear when Google searching, unlike Java and PHP where the official docs are almost always in the top couple of hits for any particular concept.

I do really like the code blocks approach, and how it easily enables functional solutions to problems. The ability to add code to classes, even if they’re standard in the language or from another library, and to do so at any point in your code, is great. I’m not totally convinced about mixing in code, but I think that’ll come with experience of Ruby. missing_method is obviously wonderful.

Unfortunately, the interpreter errors are truly astonishingly bad.

About the book

Each “day” takes a few hours, maybe 3-4. Easy enough to fit into an evening if you’re not doing anything else. In fact, you could probably do the whole “week” chapter in a day, if you had, for instance, a whole Saturday free.

However, I struggled to fit this in around other activities. Realistically I think I have the time to do maybe one language a month, maybe two at a push. Breaks such as Easter or Christmas would allow me catch up, so I may be able to finish during Easter 2013 (that would be 7 languages in 12 weeks)?

The approach of the book is interesting. It is clearly explained the introduction, but when you read that, it’s a bit difficult to envisage what it’ll be like until you dive in. What it boils down to is that this book makes you think. It gives you just enough to engage your curiosity and send you off to a search engine to find out more.

However, this can be a little frustrating at times, because an idea the book Ā demonstrates, is underlain by a concept that is familiar to you from other languages, but may have been implemented in one of several ways (which you have seen in those other languages!) For example, it is not clear how one “implements comparable” (is it as strict as in Java?) As the book cannotĀ anticipateĀ this, it is then a matter of either guessing the right question to search for (not that easy) or learning the entirety of that branch of the language well enough to answer the question yourself (a bit time consuming)!

The exercises are quite neat, but really require you to go out and learn loads of the language. While I guess it’s a good incentive to learn things, I feel like my relationship with the book is stuck in a limbo between me wanting to get a flavour for the language, but not having the time to learn large parts of the language to do the exercise.

I am enjoying it though, and will get through it, time permitting šŸ™‚

Advertisements
One Comment

Trackbacks & Pingbacks

  1. Seven Languages in Seven Weeks « Clare Hooper's Blog

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: