Throughout the year I have introduced myself to a few programming languages and I am currently on my third week with Ruby. Let me start by sharing a few things you have already heard about Ruby. More accurately, let me share my interpretation of things you have already heard. First and foremost, Ruby is fun. You’ve heard this, right? This is usually closely followed by “Ruby is intuitive and concise.” There’s a summarizing statement which gets thrown around often – just write your Ruby code the way you think it should work and it will work. I have the impression that Ruby is magical. Pepper in comments about the language being easy to read, productivity gains, beer and rainbows and you get a pretty good sense of Ruby things. That’s the pro-Ruby spiel at least. Often anti-static-language-with-tooling-support talk gets thrown into the mix too. Don’t let that last sentence give you the wrong impression. This isn’t as much bashing as merely reinforcing ones fondness of Ruby by bringing attention to what some dislike about tools and languages like Visual Studio and C#. That’s right. IDEs are for chumps! Intellisense is your crutch! I’ve got your compiler right here! Okay, I’m having some fun here, but hopefully you get my point. Ruby is enticing on its own merit but, unmistakably, there’s something about other tools/languages which gives Ruby even more appeal.
There’s a rumor that I can explain what you can do in Ruby that’s not really possible in .NET in a mere 2 minutes. Full disclosure, that’s 2 minutes, in person, over lunch. That’s two minutes of me frantically waving my arms around like a crazed lunatic. That’s me holding your head in my heads, forcing you to look me deep into my eyes in search of the truth. That’s me slamming my fists down on the table trying to scare you into seeing the light. You get the picture. With that, start the clock:
The most important thing to know is everything stated in the introductory paragraph is true. Every little bit – even the magical part. You just can’t know that until you have experienced it for yourself or you’ve known me for way too long and my arm-waving, eye-gazing, fist-slamming rants are enough to move your cheese, as they say. Give yourself three weeks and 20 Project Euler Problems and you will believe. And then you can state convincing others over lunch… Go ahead. It’ll be fun. Ruby will feel just like home and your code will just work without an IDE, Intellisence or a compiler.
As I stressed to Jon (who has worked with Ruby), the heart of Ruby is its simplicity and it is found everywhere – in variable declaration, iterations, extending classes, everywhere… And, this, my friends, you just won’t find in C#.
Want to declare a variable in Ruby?
The interpreter will figure out the type for you – even if the type needs to change at runtime. In C#, there’s var (which isn’t the same at all), there’s reflection and there’s the dynamic keyword but you can’t easily get mimic this core Ruby functionality.
Want to create a range of values 1 to 10? In Ruby, it’s
In C#, the closest you can get is
and you’re dealing with 3.5+ with Linq. Once you learn how to loop through ranges in Ruby, the thought of writing a C# For loop will make your fingers ache.
Want to extend a class? In Ruby, classes are always open so it’s as easy as
# There's no factorial method in Ruby, I guess. class Integer def factorial (1..self).reduce(1, :*) end end
I further shared with Jon how one can redefine or add to any object in Ruby. Jon’s responds was that you can do that in C# with extension methods. Of course, but we all know that extension methods only work if the C# class isn’t sealed and you can’t share the same signature as an existing method. So it’s not really comparing apples to apples…
As I work with Ruby, I can’t help but consider what Rubyists who have followed C# over the years have been thinking. Even in the context of the trivial examples above, they have to be asking why C#, by comparison, is so verbose and so complicated.
Even in the throes of my current Ruby love fest, I’m not jumping ship. I love C# and I’ll likely stick with it for a while. After all, it pays the bills. But, to say the very least, Ruby turns me on and my experience with Ruby has been so eye opening that I encourage you to experience it for yourself.