Rubyize this

It’s not a secret, Ruby syntax offers many possibilities. One thing I enjoy doing once in a while is to take some code I have written a few days before and improve it to make it look more rubyish (read : short, expressive and readable).

I thought it could be fun to make a little game out of it. I’ll put a chunk of code that needs some improvement. Try to analyze and rubyize it.

Be warned though, the code snippet of today is quite stupid. It has been written by Fleebie, the red slime with a trumpet piston shaped mouth.

That’s it. How would you rubyize this piece of code?

#UPDATE : It seems that the PRE tag tip I talked about doesn’t work. WordPress strips the pre tags when you are not an administrator. I may have to hack some php files to make it work. Someone knows a wordpress plugin that let the users write nice and indented code snippets?

Our latest project TimmyOnTime now in alpha version!

Dan and I are extremely excited to release the alpha version of our new project called TimmyOnTime.

What is TimmyOnTime?

TimmyOnTime is a IM-based time management tool. To use it, you only need a Jabber client like Google Talk or Gaim (MSN or AIM users don’t despair! We also added support for these 2 networks). To have Timmy track the time on your projects, you only have to chat with him via your IM! Let’s say for example you want to create the following new project : Achieve hapiness. Well, you just tell Timmy about it and you write : create project achieve hapiness

Timmy will create your project and tell you if everything went OK in the process. Now that you have a project, you might want to create some tasks? Nothing is easier : create task smile more often

At this point, Timmy have started the first session on your new task. To stop the timer, just write : stop

Web access is optional

That’s right. One of the important thing for Dan and me was to make optional the web registration process. So, if you don’t want to bother about the website, just use your IM and chat with Timmy. However, we’re pretty sure you’ll want the added features of a web access (project / task renaming, time editing, etc).

It’s your chance!

We want to offer to RubyFleebie and JavascriptKata readers the chance to try this brand new application first. Since it is an Alpha release, we decided to limit to 150 the number of users that can access TimmyOnTime. If everything goes smoothly, we will increase that number slowly over time.

During this alpha period, we would appreciate that you give us feedback on the application. Also, if you find bugs, it would be nice that you let us know! (

To get you started : TimmyOnTime

Ruby and Code Consistency

You know what is the one thing everybody will tell you if you ask them about the ruby programming language? They will tell you that everything is an object, bolding the “everything” like I just did. I think every rubyists in the world said this sentence at least once in their life. After telling you that everything is an object, they will probably tell you about code blocks, ducktyping or whatever cool feature ruby has to offer (that’s what I did). And finally, they will tell you that ruby gives you several ways to achieve a single thing.

This is great, I mean, this is really great. Ruby lets you express yourself, how nice is that? But that delightful feeling of freedom comes with one responsibility : you have to keep your code consistent.

Sure, you can go all funky and do that :

It works! But God… where is the consistency?

  1. On line 1, parenthesis. On line 4, no parenthesis? Hey!!
  2. Line 2 : Those great modifiers… perfect for some clean one liners! Line #4 : Why no modifier this time?
  3. Line 8 : You enjoy using do and end for your blocks? Cool! Line 12 : Hmm, now you’re using those braces just to confuse the hell out of me? The convention I use (i don’t know if it’s official or not but I read it somewhere) is to write “do” and “end” when the block takes more than one line of code. Otherwise I use braces and put the whole expression on a single line, like that : my_collection.each {|item| puts item}. But whatever your preference, the important is to stick with a convention.
  4. Line 16 : The if modifier is used in conjunction with the ! operator. Line 17 : unless modifier is used.

This kind of things won’t break your code, that’s for sure, but inconsistency is bad because it makes our applications look more random, more chaotic.

With ruby, it is so easy to write anything the way we want at the moment we are writing it. I admit I do it sometime. However, we… must… resist…

Pick a style

Recently, I began to create my own conventions. I will put some of them here just as an example. What’s important here isn’t the conventions per se, but the fact that I do my best to follow them.

  1. “unless” over “if !”
  2. Indentation : two white spaces
  3. if operation precedence isn’t an issue, no parenthesis
  4. Blocks : “do” and “end” if more than a line, “{” and “}” otherwise
  5. At the end of a function, no use of the “return” keyword (e.g. obj instead of return obj)
  6. Whenever possible, no “global” looping. In other words : “collection.each do |item|” over “for item in collection…”
  7. String concatenation : << over += (and it's more efficient!)

There are a lot more… but I don’t see the point of writing them all.

What are your conventions? How important is it for you to be consistent in your code?