Beauty in Ruby Syntax

21Dec06 Steven

Inheritance using <
I heartily enjoy Ruby’s use of characters that typically imply directionality. For example, inheritance in class definitions is expressed with the less than sign, which seems to suggest visually that the new class is receiving the contents of the class on the right. In the example below, the ApplicationController is going inside the AuthController. I suppose you could argue that the AuthController is “less than” the ApplicationController, but the presence of the word class should force you to switch contexts.


class AuthController < ApplicationController

In Java you’d write

public class AuthController extends ApplicationController

In C# it’d be

public class AuthController : ApplicationController

And in Python you’d do

class AuthController(ApplicationController)

Hashes using =>
Additionally, the use of => in Ruby hashes is really nice. When I see something like:

redirect_to :action => "list"

I see an arrow pointing to list, which is where I want that to go. Even in a plain old hash definition the => works well by basically pointing from keys to values.

site = {
     "name" => "code in focus",
     "purpose" => "talk of code and such truck",
     "founded" => 2006
}

In Python I found the hashes to be a little less exciting and directly readable. The colon is a neutral character, so each side of the key:value pair seems equal. Not really much to complain about, I just find the arrows communicate a hash more readily. For example,

site = {
     "name" : "code in focus",
     "purpose" : "talk of this and that",
     "founded" : 2006
}


No Responses to “Beauty in Ruby Syntax”  

  1. No Comments

Leave a Reply



 

Bad Behavior has blocked 14 access attempts in the last 7 days.