THE PROGRAMMING DJINN

(THE LEAST BORING PROGRAMMING BOOK TEACHES RUBY PROGRAMMING TO BEGINNERS IN AN UNCONVENTIONAL WAY. THIS IS AN EARLY EXCERPTTHINK ALPHA CHAPTER)

A djinn interrupts my morning coffee. It sweeps across the room in a blaze of uncoordinated color, and hovers near the ceiling of my study. Its bright eyes glow, and it smells of cinnamon. I want to run, to scream, to panic. I do none of these things.

It lifts its lips in a sharp-toothed smile and says, “I hear you want to learn how to program.”

“Why yes.” I shiver. What else does it know?

“Much.” The djinn smirks. “My name is Lilu, and to program is a powerful magic. I’m afraid it’s a magic you dearly need.”

“Wait — why?”

“Hmm, why indeed? Let’s say: there’s a man in your closet, and he means you no good.”

I hear a crunching sound behind me, a shuffling. I begin to turn.

“No,” says Lilu, and his words cut short my motion. “To look is to bring him into existence. First, you must arm yourself. You must learn Ruby.”

“I must learn a gem?”

“This gem is Turing complete [1], and that makes all the difference!”

* * * *

“You see, Ruby grants you access to a powerful function called puts.” Lilu says. His six-fingered hand rests upon his blue chin. “As its name suggests, puts moves things into various places. It outputs things. And consider the possibilities! You can output this evil man into a place where he can’t do you any harm. You can puts him onto the screen of your computer.” He pauses. “You do have a computer, right?”

“Of course,” I say. “I have one right here. There’s another–”

“Do not turn around!”

“No need,” I lift the screen of the laptop that sits upon my desk. At Lilu’s instruction, I open up a new file called closet.rb in my text editor, and type the following:

puts “that evil fellow in my closet”

“Lovely,” Lilu says. “Now save the file, and go back to your terminal. You can run the program with the command ruby closet.rb.”

I do as he suggests. Upon my laptop appears the following:

that evil fellow in my closet

Lilu laughs, the sound of bones cracking in a hot fire. I shiver again, and he pats my shoulder in reassurance. “You trapped him! Serves the fellow right for hiding in your closet. Now he’s stuck in a string, and just let him try to terrorize you there!”

I try to smile, but find myself unable. Lilu ruffles my hair. “Don’t worry,” he says, “you’ll soon feel better, as the power corrupts you. It’s something of an incremental process.” He lets out another grating laugh. “But let’s move forward. There are other ways we might punish this terrible fellow, much more dynamic ways.”

I open up closet.rb and make a few changes.

fate = “falls into a river”
puts “that evil fellow in my closet ” + fate

“Why is this more dynamic?” I ask.

“Because we’ve introduced a variable, fate. Think of a variable as a container into which we can place different sorts of values. Here the equal sign assigns the fate variable the string “falls into a river” then adds it to another string “the evil man” and finally prints out the result. In Ruby, applying the plus operator to two strings appends them together.”

“I see. So it’s more dynamic because we are using a variable, even if in this case the variable doesn’t change.”

“Right, now throw him in the water.”

I run the program.

that evil fellow in my closet falls into a river

“Wonderful!” Lilu says. “It’s almost as if you are omnipotent, no? And since you put the man’s fate into a variable, you can now easily change it and subject him to any number of punishments. Make the program more clear by adding in a second variable.”

As he directs me, I type a few more lines.

man = “that evil fellow in my closet”
fate = “falls into a river”
puts man + “ “ + fate

“Why did we add the space character in between the pluses and variables?” I ask.

Lilu flashes a feral smile. “Think about it.”

“Oh, I see. Whitespace is also a string, and we want a space between the two variables.”

“Clever.” Lilu winks. “But we can do better. Suppose we are too lazy to think of good punishments ahead of time. We might prefer that our program asks us what the evil man’s fate should be after it starts running. For this we have gets, a function that will read a string from the terminal.”

“Ah, don’t tell me!” I say. “Let me try on my own.”

puts “What shall his fate be?”
fate = gets
person = “that evil fellow in my closet”
puts person + “ “ + fate

I run the program again with ruby closet.rb, and this time it’s polite enough to ask me a question:

What shall his fate be?

“Let me at the keyboard,” Lilu says. His long fingers tap in short brisk strokes: click, click, click.

that evil fellow in my closet is bitten by a rabid dog.

“Is that not charming?” Lilu asks.

I nod, unconvinced. I feel a bit of sympathy for the fellow who was once in my closet.

“But that’s not the important thing.” Lilu’s bright eyes flare brighter. “You’ve built and run your first Ruby program! I introduced you to puts and gets, and you got to play a bit with variables. Now, onward. You’re about to become very rich.”

1 A system is Turing complete if its rules can simulate any other computer.