Two Minutes of Ruby

I love RubyThroughout 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?

x

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

(1..10)

In C#, the closest you can get is

Enumerable.Range(1, 10)

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.

Comments

  1. I just started learning Ruby so that I can learn Rails. I found that it is an interesting language but (and this is probably due to my lack of exp with Ruby) I can’t see how you could make useful “applications” and not just scripts. Yes, you can do websites but so can PHP. Can you build enterprise apps?

    That isn’t a knock on Ruby, i’m just asking a question.

  2. @Dustin – I don’t think I have the experienced to answer your question either. Maybe (hopefully) someone else will chime in. That said, I don’t think I’d be going too far out on a limb if I said that most Ruby hype is centered around Rails. And though you are learning Ruby as a means to learn Rails, I wouldn’t be surprised if you start digging the language enough that you turn to it for web development, random scripts over possibly other applications development in the future. I have the sense that Ruby is a tool best suited for Rails and scripting, but I’m sure it could lend itself elsewhere. For example, I’ve heard about writing MVC views in dynamic languages like Ruby and I’ve also read the benefits of using Ruby/Python for unit testing. Long answer to say, “I’m not sure. Sorry.”

  3. I have played with Ruby a touch and am becoming increasingly interested in it. Coming from php, I had a hard time adjusting to ASP.Net and all the costs associated. It pays the bills as you said. My only grumble with Ruby is the speed. I know it is getting better but speed tests show time and time again how Ruby is super slow. Look at Twitter! It is one of the slowest and highest failure rate services I have ever seen.
    I also disagree with the “Intellisense is for chumps” comment. Intellisense can help a person understand all the options available at a given point in code and therefore can help us understand what it does, even the obscure pieces. It can be a crutch, but my favorite thing is that when I select something, I KNOW it is typed correctly and it is what I intended. Being a JS lover I do not have the solid and extensive intellisense and that is fine, but I find mis-spellings and wrong casing all the time. This requires at least as many refreshes as otherwise.
    Great post though. I appreciate the opinion and look forward to working with Ruby more.

  4. Let me qualify the “super slow” comment with “super slow compared to other languages (C#, Java, Etc). Just saying super slow is a relative and ignorant comment. Lol.

  5. @Zach – Thanks for the comments. Intellisense and even the compiler, for that matter, should be appreciated and used. Heck, I publicly shared that I learned much of the core Linq syntax by playing with the various ReSharper refactorings. All are valuable learning tools and code validators, for lack of a better term. That said, the Ruby community has embraced TDD, BDD, etc in order to reduce most of those risks. TDD/BDD come with some upfront cost but there is continuous return on that investment. The compiler and intellisense offers none. Well, there’s still the learning aspect which intellisense provides but if a language is truly grokkable, maybe the learning aid is not really needed after all. And who knows? Maybe intellisense and ReSharper aren’t really teaching us anything. Maybe they are just helping us fill in the blanks until we turn to them for that same task again and again? Don’t get me wrong: I am forever thankful for VS and the tools which compliment static languages like C#, but maybe that’s because it’s all I really know? I’m kind of thinking on paper here. You know. Hands frantically waving… Fists slamming…

  6. @Zach: Twitter runs on scala now. Since early 2009.

    As I’ve been discussing with Ben, I think that getting an idea to market is your number 1 priority when creating an original web application idea. If you can’t get it out the door quickly, you’re likely to lose interest and momentum before you even see if the idea you had was a good one. Ruby lets you focus on your idea. After you are successful, you can worry about optimization/scalability/etc.

    “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil”

  7. @Zach
    Speed of front-end is just a none-issue, cause you hit the DB first and cause you can always scale horizontally, get more boxes and you’ll be ok. Facebook uses all kinds of technologies in the back-end like Erlang, C, whaterever, but they’ve used PHP for front-end until recently.

    Intellisense is really evil, cause it promotes creation of functions with tons of arguments and doesn’t let you code top-to-bottom aka “test first”. Always gets in the way with its stupid suggestions for the methods I haven’t implemented yet.

    @Ben
    Resharper is kinda evil cause with its excellent navigation no one cares to keep their assemblies/namespaces in order. Though it let’s you clean the mess in no time.

  8. Good comments, @Synapse. I have found that ReSharper actually helps me keep everything in order thanks to namespacing hints, the ability to refactor classes into their own files, etc. I’m pretty good at organizing my code anyway though so ReSharper is more of an aid than a crutch for me in this regard. I get your point though. Thanks again.

  9. @John Bristow: Doesn’t Twitter still use Rails for the front-end and use Scala for their queuing? and @ Zach Curtis, I think that invalidates your argument about Ruby being slow compared to other languages. Scala can’t handle the massive queue that twitter deals with. Has anyone here actually been responsible for scaling an application that handles as much real-time data as twitter? Be honest with yourself about the answer, they have a monumental task.

  10. @Jeremy, You’re right, Twitter is still Rails on the front-end with Scala working behind the scenes. Here’s a pretty good write up for those interested.

    @All Kind of to everyone’s point, Ruby (not to be confused with Ruby on Rails) was causing problems for Twitter. As Robey Pointer from Twitter explains in the prior reference:

    We had a Ruby-based queueing system that we used for communicating between the Rails front ends and the daemons, and we ended up replacing that with one written in Scala. The Ruby one actually worked pretty decently in a normal steady state, but the startup time and the crash behavior were undesirable. It was a little too slow and memory intensive. Sometimes our peak loads would knock it out. And when it got knocked out, it was very slow to recover, which is not what we wanted. We wanted something that could handle the edge cases and the high load, maybe not as easily as a regular load, but with relative ease.

    I could be wrong but I think you’re all right! Ruby is slow for some things and just fine for others. It all depends on the problem domain, making right choices and accepting trade-offs when they unfortunately come up.

    I guess…

closed