Ruby Golf
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
1e6is shorter than1000000. - Use Dash-Rocket Syntax for Procs
If you’re using Ruby 1.9, use the dash-rocket (
->) syntax for procssayhello = -> {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
mapis the best iterator as it has the least characters. It can be used instead ofeachbecause 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 theto_procmethod for the symbol that follows (in this case theupcasemethod 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==eis shorter thane.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!

def fizzbuzz(n)
n%3<1&&f="Fizz";n%5<1?"#{f}Buzz":f||n.to_s
end
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!
DAZ
def fizzbuzz n
n%3<1&&f=:Fizz;n%5<1?"#{f}Buzz":"#{f||n}"
end
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: https://gist.github.com/1383759
DAZ
def sum(n,t)
n*(1..t/n).inject(&:+)
end
def sum(n,t)
n*(1..t/n).inject(&:+)
end
What a dumb miss by me ...
Can this be made any shorter still?
DAZ
def sum(n,t)
n*(1..t/n).inject :+
end
three chars shorter
%w[ a b c ].map(&:upcase)
Do this:
%w[ a b c ].each(&:upcase!)
:)
https://gist.github.com/1384869
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}"
end
def caesar(f,x)
f.each_char.map{|c|t=c.ord+x;(t-1)&31>25&&t+=x<0?26:-26;t.chr}*''
end
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"))
end
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")
end
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}"
end
Keep them coming ....
DAZ
https://gist.github.com/1385465
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]
end
[ruby] def play(i)
m=%w(Rock Paper Scissors)
m[c="rand(3)"]+?,+%w(Draw Win Lose)[((m.index(i)||c-1)-c)%3]
end[/ruby]
https://gist.github.com/1384869
It would be useful if people wrote down how many bytes they had used too ...
DAZ
def count(s,x) s.scan(/#{x}/i).length; end
cheers,
DAZ
def fizzbuzz(n)"FizzBuzz#{n}"[(n%3==0?0:(n%5==0?4:8))..(n%5==0?7:(n%3==0?3:-1))] end
def sum(n,t)
k=t/n;n*k*(k+1)/2
end
Of course! Maths ftw!
DAZ
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.
cheers,
DAZ
def caeser s,n;s.chars.map{|c|(c.ord+n).chr}*'';end
==> 49 chars
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?
DAZ
https://github.com/JoshCheek/Play/tree/master/ruby-golf
-----
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;f.zip l.rotate end
1 tests: Pass
The directions didnt specify randomness as a requirement,
this algorithm is simple and deterministic but easy to understand.
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]]*','
end
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]]*','
end
def sum(n,t)
t/n*n*(t/n+1)/2
end
def sum(n,t)
n*(1..t/n).inject :+
end
Which exposes another Rubygolf tip:
Save a single character with:
meth arg
rather than:
meth(arg)