Insight Author
Ian Grant
Software Developer

First Ruby TRICK Winner - Best Pangram

Our Ruby team attended Paris.rb Conf in Paris this July. Here's a breakdown of the winner of Ruby TRICK 2013.

July 15, 2018
Marino Insights Cover Image

Recently our Ruby team attended Paris.rb Conf 2018, a Ruby conference in Paris, France. The conference boasted a panel stacked with Rails and Ruby contributors ranging from systems engineers to media artists to Thai massage enthusiasts. There was an abundance of great content that I'll be ruminating over for weeks to come, but among these talks one elicited immediate gasps of awe from the audience. 

Oohs and aahs rippled through the hall when Koichiro Eto introduced some selected entries from the TRICK (Transcendental Ruby Imbroglio Contest for rubyKaigi) contest. 

The first TRICK was held in 2013 but the winning entry "Best Pangram" was still new to most at Paris.rb. Every Ruby developer should get a chance to see these entries. 

In spite of being five years old this obscure Ruby still runs (albeit with a deprecation warning) in Ruby 2.5 (much to Bozhidar Batsov's chagrin). So here it is:

Best Pangram

This line of ruby code, written by Kazuhiro Inaba, contains each printable ASCII character once and outputs each printable ASCII character once.

Here is its output:

 

The Shape

Each character in the ASCII specification corresponds to an integer that marks its place in the table. The printable characters occupy the range between 32 to 126. The simplest way to obtain this output in ruby is arguably something like this:

This gets us the output we want, but we've already used three characters more than once: ., |, and n. On top of this we haven't used every character and we haven't been able to show off any esoteric ruby.

While not much is clear from the winning line of code, it's easy enough to see there's a while loop involved:

So if we rewrite the code to use a while loop:

Since the pangram is written on one line we can assume it uses ruby's one-liner syntax for while but that means we need to condense the expression inside the loop into one line. We can change it to putc x += 1 but this will add 1 to x before calling putc on x so we need to change our counter to begin and end one lower:

Then the one-liner while syntax:

It is clear from looking at the original pangram that the call to putc comes immediately after the while definition; inside the condition.

While it is usual (and typically recommended) for condition expressions to be idempotent they don't have to be.

Here the character is printed to the screen, the counter is incremented and the condition is checked all in one expression. This is possible because putc returns the index of the character it has just printed. The one-liner while syntax still requires there to be an expression on the left but for now the content doesn't matter:

Now we can get an idea what each part of the original pangram corresponds to in our code:

The Expression

The expression in the while loop can evaluate to anything but in this case it evaluates to true and is a handy place to dump any characters that weren't usable elsewhere. It breaks down into three expressions separated by the bitwise & and | operators:

The bitwise operators will accept only numbers or booleans but if the first term is a boolean the rest of the arguments will be coerced into booleans based on their truthiness. In this case the first term is an uninitialised attribute, denoted by @, which has been used as a place to put any otherwise unused characters with a reference to the classic English pangram "The quick brown fox jumps over the lazy dog". 

This attribute is preceded by the not operator(!) which converts the attribute to a boolean and negates it. The attribute is nil which in ruby is falsy and so after the not(!) operation returns true. This allows the next two terms to return any value not just booleans.

The next term is an array containing a regex: %r{\"}mosx, and a false expression: 4>6. This is a great opportunity to use some of the peskier special characters like the comma. %r{} is the literal syntax for declaring regexes. This one matches a doublequote character. The mosx options passed instruct the regex to:

m -> Multiline mode: includes newline(\n) characters in what is matched by .

o -> Interpolate ruby embedded in the regex with #{}, but only once

s -> Change the encoding to Windows-31J

x -> Ignore whitespace and comments in the pattern

All of which do nothing in this context.

Bonus Illegibility Tip!

The %r{} literal can use almost any pair of special characters as delimiters. This allows you to confound syntax highlighters and enrage anyone who needs to read your code with choice cuts like these.

The third expression has an important task to accomplish. It must allow the while to follow it immediately without a space as the space character is used to separate putc from its arguments. This is accomplished with a ruby relic. 

Before ruby 1.9 characters had to be declared with the character literal syntax consisting of a question mark followed by a character: ?a == 'a'. This is still supported today in ruby 2.5 for compatibility despite being almost entirely undocumented and ill-advised. 

In the pangram this allows the declaration of a single quote character. This serves to both helpfully escape the single quote, and allow the while keyword to follow immediately with no separation.

The Workhorse

This is the where most of the work gets done: it increments a variable and prints the corresponding character to the screen. 

This requires an integer variable to increment and to avoid having to initialise a variable on a separate line it uses a ruby global variable: $.. This variable holds the last read line number from the last read file. Since no files are read in this program $. starts at zero. 

This value is then incremented(+=) on each iteration by the value 9/2^5. When applied to integers the bitwise exclusive or operator(^) looks at the bits in each position of each integer and if one of the bits is 1 but the other bit is 0 it places a 1 bit in that position in the result. 2(010) XOR 5(101) returns 7(111). When both arguments are integers division in ruby returns an integer and discards any remainder so 9/7 returns 1.

So on each loop the variable $. is incremented by 1. Then all of its bits are inverted using the ~ operator. The signed integer representation used in ruby is two's complement. Because of this the effect of flipping all of the bits is to add one to the number and invert its sign, e.g: ~5 == -6. The preceding - then flips the sign back, so together - and ~ add one to a number, e.g: `-~7 == 8`

This is then summed with the 3_0. Underscores placed anywhere inside of a number in ruby are ignored and usually used to make larger numbers more readable, e.g: 2_000_000. All together putc 3_0-~$.+=9/2^5 starts printing characters at 32 and increments the character it prints on each iteration.

The end of the pangram is simple enough. It takes the value returned by putc 3_0-~$.+=9/2^5; and checks if it is less than 18*7 and if :`# evaluates to true. 18*7 is 126 which is what we expect to be here from the simplified version. The :` is a backtick symbol which is truthy because it is not nil and # opens a comment at the end of the line.

The End?

To help to follow along which parts of the code are doing what there is an interactive widget of the code below. Hover your cursor over a section of the code to reveal a tooltip with a brief description of that code's role in the program. 

While this code is far from practical, understanding it is a gateway to learning some of the nuances of Ruby which may help to diagnose an obscure error, maintain bad legacy code, or maybe even help with a creative solution to a tough problem. 

And it's so pretty!

!@THEqQUICKbBROWNfFXjJMPSvVLAZYDGgkyz&[%r{\"}mosx,4>6]|?'while(putc 3_0-~$.+=9/2^5;)<18*7and:`#

More from the blog

More blog posts