Thursday, December 14, 2006

Seven Things You'll Love about Ruby

I don't know if you got the memo yet, but next year you'll be programming in Ruby. Here's just a few of the little things you will love about Ruby:

  1. Simple programs are simple
    If you are like me, I have a stable of code snippets and little utility classes. When I start a new java program I mentally think about which old program is similiar to the new one (e.g., ok I need a program that reads a file, takes input from the user, and writes out some proccessed data to an output file). I modify the old one to create a new program. I found I don't do that with Ruby.
    For example, just to do a hello world in C#,

    using System;
    public class HelloWorld
    {
    public static void Main(string[] args) {
    Console.Write("Hello World!");
    }
    }

    In Ruby:

    puts "Hello World!"


  2. Open Classes:
    You can add methods to existing library classes. For example, in C# 2.0, Microsoft added the very helpful string function, IsNullOrEmpty() which replaces the unwieldly construct: if(mystring != null && mystring != ""). In Ruby you don't have to wait for the mavens in Redmond to decide you need a new string function, you can add it yourself.

    class String
    def NullOrEmpty?
    (self == nil || self == "")
    end
    end
    puts "test".NullOrEmpty?
    puts "".NullOrEmpty?


  3. Handy, time saving methods
    I'm amazed at how long I've done awkward constructs in Java and C# without even thinking a better way could be found. For example, in Java:

    if( x < 7 && x > 12 ) { ... }

    In Ruby:

    if x.between?(7,12) do ...

    Ruby is littered with helpful real world utilities for getting your job done.

  4. Methods can have variable number of arguments
    This can be quite time saving and reduce your lines of code.
    The following are all good calls to add():

    add 1
    add 1 3 5
    add 1 3 5 7



  5. Functions can return more than one value

    Yes, we could return a small array or structure with our multiply values, but why not just return multiple values:



    average,total = joke.getAverageRatingAndCount
    ...
    def getAverageRatingAndCount
    record = Rating.find_by_sql(["select count(*) as count,avg(rating) as average from ratings WHERE joke_id = ?",id]);
    return record[0].average.to_f , record[0].count.to_i
    end


  6. Parallel Assignment
    You can swap the values in variables without the use of an temp variable. Remember your first programming class: Swap the values in "i" and "j"? You had to use a "t" variable to store one of the values first. Not needed in Ruby.

    i = 0
    j = 1
    puts "i = #{i}, j=#{j}"
    i,j = j,i
    puts "i = #{i}, j=#{j}"

    Which produces:

    i = 0, j=1
    i = 1, j=0


  7. Ranges
    Ranges are real objects

    puts (1..10).each{|i| p i}

    Prints the numbers 1 to 10 without the syntax for(i=0;i<10;i++)

    (1..10) === 2 # this returns true because 2 is in range

    The Range object just does the right thing in its context which can reduce your lines of code and make code more readable.



These are little things, some of the big things you'll love in Ruby are Blocks and generating methods on the fly from code. More on those later.

No comments: