This story takes place when pigs spoke rhyme
And monkeys chewed tobacco,
And hens took snuff to make them tough,
And ducks went quack, quack, quack, O!

Have you ever told a story with your code?

No, I don’t mean boring user stories, I mean real, make-believe stories!

The story of The Three Little Pigs is a real story. A fable, even!

The Three Little Pigs
Illustration by Lee Sheppard.

It uses the rule of three to tell a simple story of pigs versus a wolf: each encounter sets up a scenario, which gets executed, results in a pay-off that affects the state of the story, and moves the plot forward.

But, can it be coded? Can we then use its test suite to actually tell the story? I decided to try and find out!

Nice stories deserve to be told using nice programming languages. So, I chose Ruby, since Matz, Ruby’s creator, seems like a nice person. For testing, I chose RSpec, a Ruby tool that uses expressive language to write tests: just what I needed to describe the progression of the story.

You can find the Ruby code for my interpretation of the story at The Three Little Pigs Github repository, but nothing beats having someone read you a story.

So, gather round your monitors and devices, and let’s re-visit this classic fairy tale together, which begins Once Upon A Time…

Comments