featureenvy

The blog of a thinkerer.
By @featureenvy

All rights reserved.

3 Things You Could Know If You Read "Eloquent Ruby" by Russ Olsen: A Review

Starting with Ruby isn't easy. Especially if you are like me. I have, like many before me, a Java background. So I knew statically typed languages, I knew what it meant, I knew what its strength were. And, like the monkey that I was, I thought statically typed languages were the best! Why? IDEs have a really easy time to refactor a lot of code. Everything was clear. Well, you know, the standard arguments you probably also here when we talk about statically typed languages. But this post has nothing to do with statically typed languages, with me having seen the light at the of the tunnel and all that. And since you are reading this, I guess you did the same! But, fun fact, here, for free, no sign-up needed: Ruby can do a lot more. A lot! And it has it warts, and it has its way of doing things. This isn't easy to understand when you are knew, or even when you are experienced. So here are 3 tips on what you could learn from reading Eloquent Ruby by Russ Olsen:

Strings In Ruby Are Awesome! Literally!

Ever had to write a string in Java? I feel sorry for you, because I have written way too many back in the days. Remember "This is a \"String\"" or even better, a Regex? (Hint: "\\..*", and yes, that's basically a smiley. That's also my theory that smilies are a result of Java programmers using Regex). The first string problem is easy to solve in Ruby: Just use a single quote! But even better, if you mix single and double quotes in a sentence ("And here shall "REST" Java. It's done.") we can solve this with a string literal: %q{And here shall "REST" Java. It's done.}. See? No problem. In fact, we could even replace { with '$', since we never use a '$' in our sentence! Or a lot of other special characters for that matter. The Regex (or Regexp?) we could write as /\..*/. No double escaping needed! And yes, it still resembles a smiley, I realize that. It's still a Regex after all! There is a lot more that can be done with string literals, so you should check that out in the book!

Ruby Has Hooks For A Lot Of Things

Ever written a module? Imagine that you are that module. This is carry stuff! Just think about it: You have to blindly stumble around, never knowing which class carries you around! Luckily, Ruby offers us hooks that we can use to alleviate these problems. Each module has a self.included method that you can use. It is called every time a class, well, includes this module. Even better, it even tells you which class it was! This way you can build up a list of all classes that have this module included. Doesn't sound very handy? But maybe, when the module is included, you also want to extend that class. Maybe you have a method that should not be on the class, but on the object! With self.included and other hooks, everything's possible!

How To Build Your Own DSL

DSLs. How much we all love these little things, and how much we use them each and every day. Thank you, dynamic typing! Probably you have at least seen some RSpec code. Yes, all that let, describe, and other syntactic sugar is just a DSL, implemented in Ruby. Do you want to know how to do this? Then go read the book! It has a few caveats that I can't all list here, because this is just a blog, not a book! Even better, he also mentions how to build an external DSL. An external DSL is not written in Ruby, and has to be parsed "by hand" with Ruby (without the Ruby interpreter helping). Which does have its pros and cons, as you might imagine.

Conclusion

All in all, a very good book that opened my eyes (and still does, on my second reading of it!). One of the really nice features about the book is its structure. Russ gives us first an introduction why we want to do this, then tells us how. So far so good. But then he continues: He also tells us how you can get in trouble with it. To top it off, he shows some examples of the described concept in the wild. If you are a Rubyist and you haven't yet read Eloquent Ruby, I highly suggest you do. Because Ruby can do a lot more than you might image! If you liked this post, you should follow me on Twitter and sign up for the RSS feed!