By Darren Jones

Ruby Golf

By Darren Jones

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:


  • 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!


  • 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


    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 }


    %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", 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)

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.



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.


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

Hole 2: Caesar Cipher

Implement a Caesar Shift Cipher


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.


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).


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’.


=> [["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.


  • Cyrus

    Heres a crack at Hole #1. My pre-google attempt was quite a bit longer but this one was inspired from

    • Cyrus

      In case the gist doesnt show up:

      def fizzbuzz(n)

      • Hey Cyrus,

        Great use of && and || there.

        I also like the way you’ve shaved 1 character from the tests for multiples by checking if it’s less than 1 rather than equal to zero!

        I also like the use of “#{f}Buzz”. I didn’t realise that you could do this if f wasn’t defined.

        Great stuff!


      • Marcin Mańk

        one character shorter:

        def fizzbuzz n

  • First attempt at Hole 1 (Fizz Buzz):

    def fizzbuzz(n)n%15==0?”FizzBuzz”: n%5==0?”Buzz”: n%3==0?”Fizz”: n.to_s end

    Here it is in gist form:

  • To be pedantic, e.to_s==e is not actually the same as e.is_a?(String) but will have the same effect in many cases.

    • Yes, not technically the same Rein, but I think these are the compromises that top golfers make to shave some valuable bytes of their handicap!


  • Kyle Putnam

    You can call #inject directly on Range without calling #to_a first, because it’s already an Enumerable. This shortens the sum method to 22 characters.

    def sum(n,t)

  • Kyle Putnam

    You can call #inject directly on Range without calling #to_a first, because it’s already an Enumerable. This shortens the sum method to 22 characters.

    def sum(n,t)

    • Thanks Kyle!

      What a dumb miss by me …

      Can this be made any shorter still?


      • In 1.9, you don’t need the &. (1..10).inject(:+). I prefer #reduce because it’s more intention revealing (same length). 1.8 has its own advantages, of course, like being able to use ?d for 100 in FizzBuzz (where I believe the Ruby record is something like 54 characters).

    • Hauleth

      def sum(n,t)
      n*(1..t/n).inject :+
      three chars shorter

  • Scott Gonyea

    You need to milk that Garbage Collector. Instead of this:

    %w[ a b c ].map(&:upcase)

    Do this:

    %w[ a b c ].each(&:upcase!)


  • First shot at the 4th hole: 99 bytes (only counting newlines between method body)

    def play(i)
    s=%w[Rock Paper Scissors]
    p=s.index i
    s[c language=”rand(3)”][/c]+”,#{p==c ?:Draw: p&&c==(-~p^-~c)%3?:Win: :Lose}”

  • Amadan

    Hole #2: 65 chars. Works only on [a-zA-Z].

    def caesar(f,x){|c|t=c.ord+x;(t-1)&31>25&&t+=x<0?26:-26;t.chr}*''


    • Amadan

      Oops… Gist :) Here’s solutions for 4 holes (I saw Cyrus’s solution before attempting my own, and I doubt it would have been shorter).

  • Rock, Paper, Scissors (116 chars inside def)
    def play(g)
    r,p,s=%w(rock paper scissors);a={r=>p,p=>s,s=>r};m=a.keys[rand(a.size)];m+’,’+(g==m ?”draw”:(a[m]==g ?”win”:”lose”))

  • Cyrus

    Hole 3, needs ruby 1.9:

    def play(s)
    m,q=rand(3),%w(Rock Scissors Paper);i=q.index s;q[m]+?,+(m==i ? “Draw”:i&&i!=(m+1)%3?”Win”:”Lose”)

  • Thanks Amadan for the checking part. My second shot at hole 4:

    def play(i)
    s=%w[Rock Paper Scissors]
    p=s.index i
    s[c language=”rand(3)”][/c]+”,#{p==c ?:Draw: p&&p==-~c%3?:Win: :Lose}”

  • Some brilliant entries so far guys … many of them proving the point that code golf often ends up looking like gobbledygook!

    Keep them coming ….


  • Remi
  • Antony Male

    Hole 3, ruby 1.9, 85 bytes inside the def

    def play s
    a=%w(Rock Paper Scissors Win Draw Lose);i=a.index s;r=rand 3;a[((i||0)+r-1)%3]+?,+a[i ?r+3:5]

    • Antony Male

      My bad, I miscounted. Is actually 93 bytes

  • Remi, Great approach for hole 4! I’ve further squeezed it down to 84b :D

    [ruby] def play(i)
    m=%w(Rock Paper Scissors)
    m[c=&quot;rand(3)&quot;]+?,+%w(Draw Win Lose)[((m.index(i)||c-1)-c)%3]

  • ah, hole 3, of course

  • There’s so much that can be learnt by looking through these entries! Yes they look horrible, but they also contain some very neat tricks!

    It would be useful if people wrote down how many bytes they had used too …


  • Marcin Mańk

    string counter:

    def count(s,x) s.scan(/#{x}/i).length; end

    • Christian Guenther

      Usage of ‘size’ instead of ‘length’ will save you another 2 chars ;-)

      • That’s a good spot Christian. I should have put a section in the article that it pays to learn all of the synonymous method names so that you always use the shorter ones.



  • First try at Hole 1: 84 bytes
    def fizzbuzz(n)”FizzBuzz#{n}”[(n%3==0?0:(n%5==0?4:8))..(n%5==0?7:(n%3==0?3:-1))] end

  • Kyle Dean

    For the example hole, use maths first, and you can write sum(n,t) in 17 characters.

    def sum(n,t)

  • Moshe Teutsch

    “12%3 true” should be “12%3 true”. Nice article!

    • Thanks Moshe,

      Your comment doesn’t make sense because of the way angle brackets are formatted, but I found the mistake and changed it!

      Glad you enjoyed the article.



  • def count(s,w) s.scan(/#{w}/i).size end

  • Christian Guenther

    Hole 2:

    def caeser s,n;{|c|(c.ord+n).chr}*”;end

    ==> 49 chars

  • Keep these entries coming – even if yours is longer than the current best, it is still nice to see the different approaches that people use.

    Any comments about the current entries and how they work would also be interesting.

    And any thoughts on Ruby Gold in general – good or bad?


  • Ryan Cook

    A gist for holes 1 – 3 ::

  • My solutions (I intentionally didn’t read the ones posted here until I got done, didn’t want to cheat)


    1. Fizz Buzz: 74 characters
    def fizzbuzz(n)(n%15==0?’FizzBuzz’:n%3==0?’Fizz’:n%5==0?’Buzz’:n).to_s end
    4 tests: Pass, Pass, Pass, Pass

    2. Caesar Cipher: 47 characters
    def caeser(s,n)s.gsub(/./){|c|(c.ord+n).chr}end
    2 tests: Pass, Pass

    This one seems like it probably has lots of edge cases that should be specified,
    but this satisfies the given example and works both forwards and backwards.

    3. Rock Paper Scissors Game: 104 characters
    def play(s)o=%w(Rock Paper Scissors Rock);m=rand 3;”#{o[m]},#{o[m+1]==s ?:Win:o[m]==s ?:Draw: :Lose}”end
    4 tests: Pass, Pass, Pass, Pass

    4. String Counter: 46 characters
    def count(h,n)h.upcase.scan(n.upcase).size end
    2 tests: Pass, Pass

    1. Swingers Function: 49 characters
    def swingers(s)f,l=s.transpose; l.rotate end
    1 tests: Pass

    The directions didnt specify randomness as a requirement,
    this algorithm is simple and deterministic but easy to understand.

  • My gist for hole 3:

    Or for those not willing to click a link :)

    def play(m)
    a,r,c=%w(Rock Scissors Paper),%w(Draw Lose Win),rand(4);[a[c language=”%3″][/c],r[a.index(m)-c]]*’,’

    • Dang. That got mangled. Let’s try again:

      def play(m)
      a,r,c=%w(Rock Scissors Paper),%w(Draw Lose Win),rand(4);[a[c language=”%3″][/c],r[a.index(m)-c]]*’,’

  • Tammo Freese

    sum(n,t) can be reduced to 15 characters:

    def sum(n,t)

  • Your sum(n,t) is a bit verbose and could be trimmed further whilst keeping the same algorithm (though still not as short as Tammo’s version, above):

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

    Which exposes another Rubygolf tip:
    Save a single character with:
    meth arg
    rather than:

Get the latest in Ruby, once a week, for free.