More on Why I Love Ruby

Reading this reminds me of how much I love being in the Ruby world now.

For one, most Ruby devs these days aren’t expected to be “senior” or know how Ruby internals work. Outside of the core contributors in Japan (and a few in the states), it seems like there are really only a handful of people who really understand what’s going on in the Ruby internals.

I guess this sounds bad… but you really don’t need to know much about the internals of the language for 95% of the possible use-cases for building a real-world Ruby on Rails app.

For almost all apps out there, you’re just shuffling data & markup (html/ajax/etc) back and forth between the websever. Pretty simple stuff, which is why Ruby shines in this area. (Do you really need an EJB Container for the backend of that weblog or simple CRUD application of yours? I didn’t think so.)

With Java, especially now that they are up to version 5 and have added an incredible array of features and libraries to core, it can take you years to learn the Java API and you’ll still be a novice in other parts of the framework.

Day-to-day Ruby coding is much easier. When you’re first getting started (and working with other people’s code), you may come across Ruby constructs that you’re not familiar with, making you feel slightly back in Perl-land.

Want to collect an array of ids from an array of ActiveRecord objects? I’ve seen it done this way:

  users = User.find(:all)
  ids = []
  for user in users do
    ids < < user.id
  end

That code is perfectly fine. Anyone just getting into Ruby will be able to understand what’s going on.

But you can also do this (which you’ll see from veteran Rubyists):

  users = User.find(:all)
  ids = users.collect { |user| user.id }        # Returns something like [1,2,3,4,...]

It looks a bit cryptic at first, but eventually I decided to actually figure out what was going on in the above kind of statement! :)

You might also see this written:

  ids = users.map { |user| user.id }

That’s the same thing - “map” and “collect” are aliases for one another. (not sure which is the alias of which exactly!)

It’s simply calling a “block” (in the Ruby / functional programming parlance) on each object in the users array. Each block call can return something (in this case, an id), which then gets stuffed into the array that’s returned by the collect method. (in this case, an array of ids)

When you discover little shortcuts like these, it makes you love the Ruby language even more!

You can Bookmark this entry on del.icio.usbookmark this, digg this entrydigg this or check the See this page in technoraticosmos

2 Responses to “More on Why I Love Ruby”

  1. Gravatar Icon 1 Shane Vitarana 

    And the hardcore veterans do:

    ids = User.find(:all).collect(&:id)

    :)

  2. Gravatar Icon 2 Shanti A. Braford 

    Hehe… I hope I don’t have to maintain hardcore veterans Ruby code in the future =)


Shanti A. Braford blogs here.

If you really want to know, just read this.



  

Powered by FeedBlitz