Tips and Tricks for Code Golfing in Ruby

Unicode compression trick:

eval’Ruby code in utf16’.encode(‘UCS-2BE’).b

Can you explain what this trick is doing?

Also, can you use code blocks when posting code? Either with the triple backticks ``` or with [code].

1 Like

This trick allows you to exploit the fact that codingame counts characters rather than bytes. Since programming languages only need utf8, you can write any code into half the space by merging every two characters (bytes) into a single utf16 character. The code is technically the same length, but counted as half.

You still need some utf8 written overhead, but in some challenges it pays off.

I only found out about this trick today, and am fairly confident it stopped working since @Westicles posted it.

1 Like