Readable Riddle in Ruby!

One of the main things that attracted me to the ruby language was how readable my code was, while at the same time retaining a tight structure and, most importantly, still being very fast. I decided to put this to the test by creating a piece of code that literally read like a passage of English text. The focus of this experiment was the “As I was going to St Ives” riddle. I’ll go into a bit more detail on the riddle itself in a second if you’re not familiar with it. The challenge here was not only to create “literally” readable code, but also to follow ruby best practices. Of course, the code would actually need to solve the riddle too 🙂

If you haven’t heard this riddle before, take a look at this and mull it over.

As I was going to St Ives
I met a man with seven wives
Every wife had seven sacks
Every sack had seven cats
Every cat had seven kits
Kits, cats, sacks, wives
How many were going to St Ives?

So exactly how readable was I able to get the code? Well, in a word, very.

And, check it out, it solves the riddle when run too!

Now, this is the point where the pedantic among you will point out that are multiple solutions to the riddle. I did not know about these other solutions when i first embarked on this experiment, basically growing up and hearing this riddle I always thought it was just a smartarse way of getting people to do maths for no reason, and that the answer was “one person, me”!

It turns out that there are actually a whole bunch of solutions based on the inherant grammatical ambiguity of the problem itself. For example, you could assume that all items and people encountered were also going to st ives. You might infer that “how many” applies only to sentient beings. You might also assume that the dude you met who had seven wives was alone, and just “told you about his wives” when you met him.

I filled my “some-facts” file with all sorts of superfluous code, almost parodying the amount of superfluous facts that are inherrent in the riddle itself if you assume that the correct answer is “one”. My personal favourite method was this one;

def as_i_was_going_to(place)
  @people_and_where_they_were_going[:me] = place

Having said that, I believe that it is possible to leave the “stives” file above completely untouched, and with only minimal changes to “the-facts” you could get the code figuring out whichever solution you wanted to interpret. In fact, I’m putting that out there as an open challenge to any bored coders or rubyists that fancy having a crack at it – code is available in the following git repo;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s