Ruby golf is the art of writing code that uses as few characters as possible. The idea originates in the world of Perl (where it is, unsurprisingly, known as Perl Golf). As a language, Perl is well suited to this as it has more than its fair share of bizarre constructs and syntactic sugar. Ruby also contains some large dollops of syntactic sugar which makes it suited to the odd round of golf.

The obsession with trying to minimise the number of bytes used in a program harks back to a bygone age when memory was at a premium and every byte counted. It doesn’t really have any practical use in today’s world, but this does not mean that it is not still used. Code minimisation can be found in competitions such as the 1k JavaScript competitions and Perl Apocalypse. There are also a number of sites dedicated to the art of Code Golf.

There is often a competitve element associated with Code Golf: Hackers try to outdo each other as they strive to find the ‘perfect’ solution. One that achieves the aim of the code but could not possibly be done in fewer characters. I find this to be analagous to mathemticians who search for ‘elegant proofs’ and am reminded of Einstein’s quote “Everything should be made as simple as possible, but not simpler”.

Unfortunately, the code that is produced is often very difficult to understand as it usually relies on various hacks and shortcuts. In fact, a complimentary game is to try and figure out what the code produced actually does! There is often a thin line between being clever and too clever…

Code Golf has a bit of a marmite reputation – people either love it or hate it, so to try and maintain some balance, I’ve listed a few of the advantages and disadvantes of trying your hand at a round or two:

Advantages

  • You get to learn some lesser known parts of the language and some neat tricks.
  • You get a nice smug feeling inside when you manage to chip a few more characters off the best solution.
  • Sometimes the more succinct way of writing the code actually looks cleaner.
  • You can learn techniques that could be of use in other situtations.
  • The process itself encourages ingenuity.
  • It’s fun!

Disadvantages

  • The code usually looks horrible
  • The code can often be difficult to read or understand, which is a shame because one of the best aspects of Ruby is how readable it is.
  • The code would be a maintenance nightmare – even by yourself never mind others.

Pro Tips

If you fancy having a go at some golf, then here are a few tips that can be used to cut your Ruby code down to size and reduce your golfing handicap:

  • Mass Assignment

    This is an easy one that most people will know, but assigning all your variables at once will help to cut down on code bloat.

    a,b = 1,2
  • Create Arrays Using the Shortcut Notation %w

    You can create arrays of strings by using the following notation:

    a = %w(a b c) => ["a","b","c"]
  • Use the Ternary Operator for Logic
    a>10?"too big":"fine"
  • Use Chained Ternary Operators for More Complex Logic
    a<0?"no negatives":(a>10?"too big":"fine")
  • Use Scientific Notation for Numbers

    If you need big numbers, then 1e6 is shorter than 1000000.

  • Use Dash-Rocket Syntax for Procs

    If you’re using Ruby 1.9, use the dash-rocket (->) syntax for procs

    sayhello = -> {p "hello"}
    sayhello = -> name {p "hello #{name}"}
  • 1 Character Strings

    For 1 digit strings, use ?x = "x" (again, this only works in Ruby 1.9 though)

  • Learn Regular Expressions

    Regular expressions can express some complicated expressions in a few characters.

  • Use the Modulo Operator to Test if a Number is a Factor

    12%3==0, because 12 is a multiple of 3
    13%3!=0, because 13 is not a multiple of 3

    **EDIT**

    This has been improved further by Cyrus in the comments. You can actually just test if the answer is less than one rather than equal to zero:

    12%3<1 => true
  • Use Map to Iterate

    Iterators are usually better than for loops and map is the best iterator as it has the least characters. It can be used instead of each because you don’t have to change each element in the array, it still loops through it.

    %w(a b c).map{|x| puts x}
  • Use Symbol To Proc

    This saves a lot of time (and possibly looks neater).

    %w(a b c).map{ |e| e.upcase }

    becomes

    %w(a b c).map(&:upcase)
    => ["A", "B", "C"]

    What actually happens is that & calls the to_proc method for the symbol that follows (in this case the upcase method is called on each element of the array.

  • Easy Joins
    %w(a b c)*"-" is the same as %w(a b c).join"-"
    => “a-b-c”
  • Reuse Loops

    A good way to avoid using 2 loops for different object types is to bung all the objects into a single array and use just one iterator, but then perform different tasks depending on the object type.

    ["a","b",2,4].map{|e|(e.to_s==e)?(e.upcase):(e*2)}
    => ["A", "B", 4, 8]
  • Testing Types

    If you want to test if an object is a string then e.to_s==e is shorter than e.is_a? String.

Do you have any other tips to bring one’s handicap down? Leave your tips in the comments.

Almost Sinatra

Konstantin Hasse (Sinatra Jedi Grand Master) performed an extreme form of Ruby golf when he condensed Sinatra (not exactly bloated at 1646 lines of code) into a measly 8 lines of code. It didn’t quite have the same functionality, but came pretty darn close. He used some great tricks when doing this, the last couple of tips in the list above are from the Almost Sinatra code.

An Example Hole

As a example, I tried writing a method that would find the sum of all multiples of a given number up to a given value. For example sum(5,24) would calculate the sum of all the multiples of 5 up to 24 (ie 5 + 10 + 15 + 20).

This is what I came up with in the end:

def sum(n,t)
  n*(1..t/n).to_a.inject(&:+)
end

I utilised the symbol to proc notation to use the inject method to sum the integers. How many integers to sum was found by doing integer division and relying on the fact that remainders are ignored.

This contains 27 characters (not including the method definition). Can anybody beat it? Leave your answer in the comments if you can.

Competition

Now it’s time to find out who is the Tiger Woods of the Ruby World. Below are five ‘holes’ that make up the RubySource Golf Course. Try your hand at any or all of them and post your solutions in the comments.

Hole 1: Fizz Buzz

Given a number the function returns “Fizz” if it is a multiple of 3, “Buzz” if it is a multiple of 5 and “FizzBuzz” if it is a multiple of 15. If the number is not a multiple of 3 or 5 then the number is returned as a string.

Example:

fizzbuzz(3) => "Fizz"
  fizzbuzz(10) => "Buzz"
  fizzbuzz(45) => "FizzBuzz"
  fizzbuzz(31) => "31"

Hole 2: Caesar Cipher

Implement a Caesar Shift Cipher

Example:

caeser("hello",3) => "khoor"

You should also be able to produce negative shifts.

Hole 3: Rock,Paper,Scissors Game

Write a simple method that ‘plays’ this game, where the player enters their ‘move’ as an argument to the method. If the player enters an invalid option then the result should be ‘lose’. The computer should choose its move at random. The output gives the computer’s ‘move’ and the result as a comma-separated string.

Example:

play("Rock") => "Rock,Draw"
    play("Paper") => "Rock,Win"
    play("Scissors") => "Rock,Lose"
    play("Soap") => "Paper,Lose"

Hole 4: String Counter

Write a method that when given a string and substring, returns the number of times the substring occurs in that string (ignoring case).

Example:

count("Banana","a") => 3
    count("RubySource provides advice, tutorials, commentary, and insight into the Ruby and Rails ecosystem","ruby") => 2

Hole 5: Swingers Function

Write a function that replaces ‘putting your keys in a tin’. The argument to the function is an array of arrays that contain two objects. The function returns a new array where the pairs of objects have been mixed up. An object should not end up with it’s original ‘partner’.

Example:

swingers([["Homer","Marge"],["Micky","Minnie"],["Fred","Wilma"],["Peter","Lois"],["George","Judy"]])
=> [["Homer","Wilma"],["Micky","Lois"],["Fred","Judy"],["Peter","Marge"],["George","Minnie"]]

To enter, write your method in the comments below. The person whose entry contains the lowest number of characters will win each hole. There’s a Sitepoint book up for grabs for the winner of each hole. You can use Ruby 1.8 or 1.9. The deadline is 31st December 2011. Only the characters inside the method definition will be counted, so my example hole above would count as 27 characters. Feel free to post a Gist to your code.

FORE!

Sponsors