|Updated

Ruby Golf

Share this article

Key Takeaways

  • Ruby Golf is a coding practice that aims to write the smallest possible code to solve a problem, originating from Perl Golf.
  • Participating in Ruby Golf can enhance programming skills, encourage creative thinking and problem-solving, and provide exposure to lesser-known parts of the language.
  • Code Golf often involves a competitive element, where programmers strive to find the most efficient solution in the fewest characters.
  • The code produced in Ruby Golf is often hard to understand due to the use of various hacks and shortcuts, and it’s not typically used in regular programming work.
  • The author provides several tips to reduce Ruby code size, such as using mass assignment, ternary operators, scientific notation for numbers, and regular expressions.

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!

Frequently Asked Questions (FAQs) about Ruby Golf

What is Ruby Golf and why is it important?

Ruby Golf is a fun and challenging game that programmers play to test their coding skills. The objective is to write the shortest possible code to solve a given problem. It’s important because it encourages programmers to think creatively and efficiently, and to explore the full range of Ruby’s capabilities. It’s also a great way to learn new techniques and improve your coding skills.

How can I get started with Ruby Golf?

To get started with Ruby Golf, you’ll need a basic understanding of Ruby programming. From there, you can start by trying to solve simple problems with the shortest possible code. There are many online resources and communities where you can find problems to solve and compare your solutions with others.

What are some tips for golfing in Ruby?

Some tips for golfing in Ruby include using single-character variable names, taking advantage of Ruby’s built-in methods, and using shorthand syntax whenever possible. It’s also important to remember that readability and maintainability are not priorities in Ruby Golf – the goal is to write the shortest possible code, not the most elegant or efficient.

How can I improve my Ruby Golf skills?

The best way to improve your Ruby Golf skills is through practice. Try to solve a variety of problems and compare your solutions with others. You can also study the solutions of more experienced golfers to learn new techniques and strategies. Participating in Ruby Golf competitions can also be a great way to improve your skills and challenge yourself.

Are there any resources or communities for Ruby Golf?

Yes, there are many online resources and communities for Ruby Golf. Websites like Code Golf Stack Exchange and GitHub offer a wealth of problems to solve and solutions to study. There are also many online forums and discussion groups where you can connect with other Ruby Golfers and share tips and strategies.

What are some common mistakes in Ruby Golf?

Some common mistakes in Ruby Golf include overcomplicating the problem, not taking full advantage of Ruby’s built-in methods, and focusing too much on readability and maintainability. Remember, the goal is to write the shortest possible code, not the most elegant or efficient.

Can Ruby Golf help me in my regular programming work?

While the techniques used in Ruby Golf are not typically used in regular programming work, the game can still be beneficial. It encourages creative thinking and problem-solving, and it can help you become more familiar with Ruby’s capabilities. However, it’s important to remember that readability and maintainability are crucial in regular programming work.

Is Ruby Golf only for experienced programmers?

No, Ruby Golf can be enjoyed by programmers of all skill levels. While it can be challenging, it’s also a fun and engaging way to improve your coding skills. Even if you’re a beginner, you can still participate and learn a lot from the experience.

Can I participate in Ruby Golf competitions?

Yes, there are many online Ruby Golf competitions that you can participate in. These competitions can be a great way to challenge yourself, improve your skills, and connect with other Ruby Golfers.

Where can I find more information about Ruby Golf?

You can find more information about Ruby Golf on websites like Code Golf Stack Exchange, GitHub, and SitePoint. These sites offer a wealth of resources, including problems to solve, solutions to study, and communities to join.

Darren JonesDarren Jones
View Author

Darren loves building web apps and coding in JavaScript, Haskell and Ruby. He is the author of Learn to Code using JavaScript, JavaScript: Novice to Ninja and Jump Start Sinatra.He is also the creator of Nanny State, a tiny alternative to React. He can be found on Twitter @daz4126.

rubygolf
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week