Chad Perrin: SOB

2 October 2007

When does ( ?? == 63 ) ?

Filed under: Geek,Humor — apotheon @ 03:56

I stumbled across a capricious little bit of Ruby-fu today. The author of this fun snippet is Jay Phillips, and it looks like this:

def wazup?(a) puts "#{a} dat!" end
wazup?!!!?!??1:!!??

When you run that code, it produces the following output:

true dat!

I racked my brains for a bit trying to figure out what’s going on here. I’ve finally figured it out. I started out just reading the method definition — that’s the easy part:

def wazup?(a)
  puts "#{a} dat!"
end

All it does is take an argument and stuff it into an output string so that when fed foo, it outputs “food dat!” Obviously, it’s getting fed “true” when the method is called.

The next line is where the fun happens, of course. First off, it’s useful to space things out a little bit to show how code would likely be formatted if the intention here wasn’t just to make it look cool:

wazup? !!!?! ? ?1 : !!??

That’s a ternary operator, with !!!?! as the boolean tested argument, ?1 as the “if true” return value, and !!?? as the “if false” return value. This could be rewritten thusly (losing pretty much all the fun of the way it was initially written):

if !!!?!
  wazup? ?1
else
  wazup? !!??
end

Those of us who are programmers should all know that leading exclamation points translate to negation of whatever they lead — like a leading exclamation point means “not”. Those of us who are logicians should all know that “not not” is the same as no “not” at all, and “not not not” is the same as a single “not”. Thus, in further translation:

if !?!
  wazup? ?1
else
  wazup? ??
end

This is where I hit my first hitch in trying to figure out what was going on. It probably took me all of about five seconds to get this far. It took me a bit longer to get farther. It’s not difficult — it just requires one to know a feature of Ruby that I had not yet encountered (and remembered having encountered).

I played around in irb trying to figure out what was going on. I considered the fact that either ?! or ?? must be getting evaluated as “true” when passed to the wazup? method, and that whichever of them was being sent to wazup? is determined by whether ?! is true or false in a boolean context. The fact there was an exclamation point in front of ?! (making it !?!) means that the usual evaluation of the ternary operator is being reversed, so that if ?! is true it returns ?? instead of ?1, and vice versa.

Feeding each of ?!, ?1, and ?? to irb evaluates to (respectively) 33, 49, and 63:

if !33
  49
else
  63
end

Everything in Ruby evaluates to “true” in a boolean context except “false” and “nil”. That means that you could effectively read this, for purposes of passing the result of the ternary operator to the method wazup?, as:

if !true
  true
else
  true
end

. . . or:

if false
  true
else
  true
end

All that was missing was figuring out why (for instance) ?? evaluated to 63. I figured out that incrementing that digit in ?1 kept increasing the evaluation, so that ?2 == 50, ?3 == 51, and so on. It dawned on me that I was looking at ASCII values, then.

A little googling later, and I discovered what I was beginning to suspect: ? prepended to a single ASCII character evaluates to that character’s ASCII decimal integer value. The mystery was solved. Of course, now it has probably taken longer for me to chronicle my thoughts in the process of figuring that out than it did to think them, but such is life. I feel enlightened.

Not so difficult, but fun nonetheless:

wazup?!!!?!??1:!!??

true dat!

2 Comments

  1. […] Ruby usually makes it easier to create more legible code, not the opposite. […]

    Pingback by Chipping the web - SOS -- Chip’s Quips — 5 October 2007 @ 02:54

  2. […] [RUBY] When does ( ?? == 63 ) ? […]

    Pingback by Best of Feeds - 34 links - programming, google, lifehacks, ruby, funny « Internet Duct Tape — 7 October 2007 @ 08:47

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

All original content Copyright Chad Perrin: Distributed under the terms of the Open Works License