6.

Downtown

[o]

Lost in Wixl This heist goes all the way up?

Oblivious to their involvement in the expansive plan of The Originals, both the tall fox and the much shorter fox had wandered right into the red alert zone, the city Wixl. I desire a spatula to scoop them aside with, shuffle them off to the coast near the beach hatcheries, hide them in piles of fish eggs, hold down their pointy ears, concealing their luxurious hides. And above them I would stand, casting an unmoving shadow, holding my rifle aloof.

I can’t. I have you to teach. I have to groom and care for myself. The lightbulbs upstairs need changing. A free pack of halogen lightbulbs just showed up out of the mail. Somebody out there is obviously trying to get me to use them. So I’m going to screw ‘em in. And just stand there, casting an unmoving shadow, holding my rifle aloof.

Should that shadow be nice and defined, then I’ll keep ‘em.

1. If I Were Looking For a Vehicle

Sitting for a moment.

I like seeing these two out in the wild. They got pretty bored here in the studio. They started making up wierd slogans and stuff. They had some phrase they kept repeating, forming fixations upon. You can’t be exposed to all that contrived fox nonsense.

Let’s just say: I am really trying my best to keep things collegiate. Having never attended college, I can’t well say if every passage written chimes right with the stringent criteria which academia demands. I have university friends aplenty, some who tour the globe in their pursuits, and I try to inflect my voice with just their blend of high culture.

Sometimes I applaud myself for going beyond the work of my educated friends - only in quiet corridors, we never butt heads publicly - because I have actually subscribed to a school of thought while they are still in their books, turning and turning.

I am a preeventualist. I have dabbled in it long enough and am glad to come forth with it. Inevitably, some of you have already started mining this book for Marxist symbology. I am sad to kill those interpretations, but I believe any nihilist conclusions you’ve drawn will still hold up under scrutiny.

Anyway, I’ll drop the rhetoric. I only mention preeventualism because, aside from being a refreshing and easy alternative to the post-modernism we’re born with, this meta-cult offers a free lost-and-found service for the residents of Wixl.

 require 'open-uri'
 open( "http://preeventualist.org/lost" ) do |lost|
   puts lost.read
 end

I have no way of alerting the foxes to this service. And I’m sure it’s too soon for their truck to be listed. Still, the good intentions are here.

If you’re connected to the Internet, the above Ruby should have downloaded the web page from the Internet and printed it to the screen. In a message resembling this:

                   THE PREEVENTUALIST'S LOSING AND FINDING REGISTRY
          (a free service benefiting the ENLIGHTENED who have been LIGHTENED)

                                      ---
                      updates are made daily, please check back!
                                      ---

                         this service is commissioned and
                     subsidized in part by The Ashley Raymond
                                Youth Study Clan

                                      ...
                      all seals and privileges have been filed
                under the notable authorship of Perry W. L. Von Frowling,
          Magistrate Polywaif of Dispossession.  Also, Seventh Straight Winner
                   of the esteemed Persistent Beggar's Community Cup.
                                      ...

  ABOUT THE REGISTRY
  ==================
  Hello, if you are new, please stay with us.  A brief explanation of our service will 
  follow.  First, a bit of important news from our beloved magistrate.  (The kids call
  him Uncle Von Guffuncle. Tehe!)

  IMPORTANT NEWS
  ==============
  / 15 April 2005 /
  hi, big news.  we were on channel 8 in wixl and ordish.  cory saw it.  i was on and 
  jerry mathers was on.  if you didn't see it, e-mail cory.  he tells it the best.  all 
  i can say is those aren't MY hand motions!! (joke for people who watch channel 8.)  
  thanks harry and whole channel 8 news team!!
                                                   - perry

  / 07 April 2005 /
  we're all sifting through the carpet here at hq, but if you could all keep an eye out 
  for caitlin's clipboard, she's too quiet of a gal to post it and i know that it's 
  REALLY important to her.  she had a few really expensive panoramic radiographs of her 
  husband's underbite clipped to a few irreplacable photos of her husband in a robocop 
  costume back when the underbite was more prominent.  she says (to me), "they'll know
  what i mean when they see them."  i don't know what that means.  :(

  i've checked: * the front desk * the hall * the waiting area * the bathroom * the candy 
  closet * the big tv area * the lunch counter * the disciples room * gaff's old room
  (the one with the painting of the cherry tree) * the server room * staircase.  i'll 
  update this as i find more rooms.
                                                   - love, perry

  / 25 Feb 2005 /
  server went down at 3 o'clock.  i'm mad as you guys.  gaff is downstairs and he'll 
  be down there until he gets it fixed. :O -- UPDATE: it's fixed, back in bizz!!
                                                   - perry

  / 23 Feb 2005 /
  i know there's a lot of noise today.  stanley bros circus lost twelve llamas and a
  trailer and a bunch of Masterlocks and five tents.  they're still finding lost stuff.
  pls keep your heads, i need everyone's help.  these entertainers have _nothing_.  i 
  mean it.  i gave a guy a purple sticker today (it's just something i like to do as a
  kind gesture) and he practically slept on it and farmed the ingredients for pizza sauce
  on it.  they are on rock bottom.

  so please donate.  i know we don't have paypal or anything.  so if you want to donate,
  just post that you found something (a children's bike, a month of perishable canned 
  goods) and that it has the circus people's names written on it or something.
                                                   - great, perry

  / 15 Nov 2004 /
  preeventualist's day sale.  if you lose something today, you get to pick one free 
  item (of $40 value or less) from the house of somebody who found something.  we're 
  having so much fun with this!!  this is EXACTLY how i got my rowing machine last year
  and i LOVE IT!!
                                                   - perry

I think the Youth Study Clan is doing a great job with this service. It’s a little hokey and threadbare, but if it can get animals to stop using their instinctive means of declaring ownership, then hats off.

Still, a preeventualist youth group? How can that be? You’ve got to at least flirted with real cynicism before you can become a preeventualist. And you definitely can’t attend school. So, I don’t know.

Going back to the list of instructions from the Preeventualist’s Losing and Finding Registry.

 USING THE L&F SERVER
 ====================
 The L&F is a free service.  The acts of losing and finding are essential qualities in
 building a preeventualist lifestyle.  We hope to accomodate your belief.

 We do not use HTML, in order to simplify our work here.  Our guys are already working
 fifteen hour days.  (Thanks, Terk!!  Thanks, Horace!!)

 You may search our service for your lost items.  Or you may add your lost (or found)
 item to our registry.  This is done by typing the proper address into your browser.

 SEARCHING
 =========
 To search for lost items, use the following address:

   http://preeventualist.org/lost/search?q={search word}

 You may replace {search word} with your search term.  For example, to search for "cup":

   http://preeventualist.org/lost/search?q=cup

 You will be given a list of cups which have been lost or found.

 If you want to search for only lost cups or only found cups, use the `searchlost' and
 `searchfound' pages:

   http://preeventualist.org/lost/searchlost?q=cup

I’m not playing games. I know where the truck is. Really, I’m not teasing you. I’ll show you in just a sec. I’m just saying, look at the foxes:

Hummmmm.

They are helpless. And yet, here is this great tool. A possible key to getting out of this mess. I just want to poke around, see if there are any clues here.

 require 'open-uri'

 # Searching all found items containing the word `truck'.
 open( "http://preeventualist.org/lost/searchfound?q=truck" ) do |truck|
   puts truck.read
 end

I’m not seeing anything about the tall fox’s truck in this list. That’s okay. The foxes are out of it anyway. We have some time.

You’ve learned a very simple technique for retrieving a web page from the Internet. The code uses the OpenURI library, which was written by one of my favorite Rubyists, Akira Tanaka. He’s simplified reading from the Internet so that it’s identical to reading a file from your computer.

In a previous chapter, we stored your diabolical ideas in a text file. You read these files in Ruby using open.

 require 'open-uri'

 # Opening an idea file from a folder on your computer.
 open( "folder/idea-about-hiding-lettuce-in-the-church-chairs.txt" ) do |idea|
   puts idea.read
 end

Files are input-output objects. You can read and write to a file. In Ruby, all IO (input-output) objects have read and write methods. The open method slides an IO object down the chute into a block for your use. IO is your ticket to the outside world. It’s the rays of sunlight cast through the prison bars. (However, you can’t write to a web page with OpenURI. You’ll need to find a tool for copying to your web server. An FTP program, for instance.)

If someone wants to read your diabolical idea about hiding lettuce in the church chairs, assuming you’ve posted it as a web page:

 require 'open-uri'

 # Opening an idea file available on a web site.
 open( "http://your.com/idea-about-hiding-lettuce-in-the-church-chairs.txt" ) do |idea|
   puts idea.read
 end

The OpenURI library also understands FTP addresses as well. This widens the possibilities for where you can store files. On your system or elsewhere on the Internet.

Reading Files Line by Line

When you’re using OpenURI to get information from the web with the open and read methods, the page is given to you as a String. You can also read the page one line at a time, if you’re searching for something. Or if the page is big and you want to conserve your computer’s memory.

 require 'open-uri'
 open( "http://preeventualist.org/lost/searchfound?q=truck" ) do |truck|
   truck.each_line do |line|
     puts line if line['pickup']
   end
 end

The above code will retrieve the list of trucks found by preeventualists, then display only those lines that actually contain the word ‘pickup’. That way we can trim out the descriptions and look for only the pertinent lines.

Above, the index brackets are used on a string, so the string is searched for whatever is inside the brackets. Since the string 'pickup' is inside the brackets, the line string is searched for the word “pickup”.

On being funny.

When a web page is loaded with read, the entire page is loaded into memory. Usually this only takes up a few thousand bytes. But if a page is big (several megabytes), you’ll probably want to use each_line, which loads one line at a time to avoid exhausting memory.

Yielding is Kiddie Blocks

Ruby often uses iterators in this fashion. Yes, iterators are used for cycling through each item in a collection of items, such as an array or hash. Now look at an IO source as a collection of lines. The iterator can crawl that collection of lines.

 class IO
   # Definition for the each_line method.  Notice how it has no
   # argument list.  Blocks don't need to be listed as arguments.
   def each_line
     until eof?        # until we reach the end of the file...
       yield readline  # pass a line into the block
     end
   end
 end

The yield keyword is the easiest way to use a block. One word. Just like a curtain has a pullstring or like a suitcase has a handle. Inside a method, you can press the blinking yield button and it will run the block attached to that method. Glowing a strong red color until the code inside the block is done. And then it goes back to blinking and you can press the button again if you like.

 def yield_thrice
   yield
   yield
   yield
 end

Punch the yield button three times quick and the block gets to live its life three times.

 irb> a = ['first, birth.', 'then, a life of flickering images.', 'and, finally, the end.']
 irb> yield_thrice { puts a.shift }
 # prints out:
 #   first, birth.
 #   then, a life of flickering images.
 #   and, finally, the end.

The shift method pulls the first item off an array. The barber shift cuts the hair off and hands it over. Then, the scalp. And just keeps going, whittling the poor guy down to nothing.

You’ve seen blocks attached to methods. Any Ruby method can have block attached to the end.

 # The brief style of attaching a block to a method.
 # Here the block is surrounded with curly braces.
 open( "idea.txt" ) { |f| f.read }

 # The verbose style of attaching a block to a method.
 # Here the block is surrounded with `do' and `end'
 open( "idea.txt" ) do |f|
   f.read
 end

If you pass arguments to yield, those arguments will also be passed to the block. The block is riding in a little sidecar attached to the method’s motorcycle. The method yells out a list arguments, screaming to the block over all the wind as they’re racing through the desert. The block taps his helmet like, “I get it, my brain gets it.”

 # The method opens two files and slides the resulting IO objects down the
 # chute to an attached block.
 def double_open filename1, filename2
   open( filename1 ) do |f1|
     open( filename2 ) do |f2|
       yield f1, f2
     end
   end
 end

 # Prints the files out side-by-side.
 double_open( "idea1.txt", "idea2.txt" ) do |f1, f2|
   puts f1.readline + " | " + f2.readline
 end

You may also wonder what the yield keyword has to do with street signs. And really, it’s a good question with, I believe, a good answer. When you run a method, you are giving that method control of your program. Control to do its job and then come back to with an answer.

With yield, the method is stopping at the intersection, giving control back to you, to your block. The method is letting you do your work before resuming its work. So while the each_line method does the work of actually reading lines from a file, the block attached to the each_line method is handed the line itself and gets a chance to hammer away at it in the sidecar.

Preeventualism in a Gilded Box

You’ve learned so much about OpenURI and using yield to write your own iterators. You know your way around the lost-and-found service. Really, you can starting hunting through the Wixl junk drawer without me.

Let’s neatly encapsulate the entire service into a single class.

 require 'open-uri'
 module PreEventualist
   def self.open page, query
     qs = 
       query.map do |k, v|
         URI.escape "#{ k }=#{ v }" 
       end.join "&" 
     URI.parse( "http://preeventualist.org/lost/" + page + "?" + qs ).open do |lost|
       lost.read.split( "--\n" )
     end
   end
   def self.search word
     open "search", "q" => word
   end
   def self.searchlost word
     open "searchlost", "q" => word
   end
   def self.searchfound word
     open "searchfound", "q" => word
   end
   def self.addfound your_name, item_lost, found_at, description
     open "addfound", "name" => your_name, "item" => item_lost, 
                      "at" => found_at, "desc" => description
   end
   def self.addlost your_name, item_found, last_seen, description
     open "addlost", "name" => your_name, "item" => item_found,
                     "seen" => last_seen, "desc" => description
   end
 end

At some point with your code, you need to start shaping it into something neat. Save the above module in a file called preeventualist.rb.

This module is a very simple library for using the Preeventualist’s service. This is exactly the way libraries are written. You whip up a module or a class, store it in a file, and, if you’re happy with it and want the world to benefit, put it on the web.

These stragglers can you use your module just like I used OpenURI earlier.

 irb> require 'preeventualist'
 irb> puts PreEventualist.search( 'truck' )
 irb> puts PreEventualist.addfound( 'Why', 'Ruby skills', 'Wixl park',
        "I can give you Ruby skills!\nCome visit poignantguide.net!" )

2. Meanwhile, The Porcupine Stops To Fill-Up

The porcupine pays for gas.  Kites?

3. A Sponsored Dragon-Slaying

The slayer hops in.

“Look around,” said Fox Small. “Some of us don’t have time for quests. Some of us have major responsibilities, jobs, so on. Livelihood, got it?”

Heyyyy, my JOB was to kill the drgn!!” screamed the wee rabbit, blinking his eyes and bouncing frantically from tree to tree to pond to pond. “His snout was a HUGE responsibility!! His smoky breath was mine to reckon with!! I spent fifty dollars on the cab JUST to get out there, which was another huge huge ordeal. You have nothing on me, not a single indictment, my whole HERONESS is absoflutely unimpeachable, my whole APPROACH is abassoonly unapricotable, just ask Lester.”

“Who’s Lester?” said the Fox Small.

“Lester’s my cab driver! He parked at the base of Dwemthy’s Array!!” The rabbit ricocheted madly like a screensaver for a supercomputer. “Just ask Dwemthy!!”

“Well,” said the Fox Small. He turned back to look up at Fox Tall, who was sitting straight and looking far into the distance. “Wait, they have a parking lot on Dwemthy’s Array?”

“YEP!! And a pretzel stand!!”

“But, it’s an Array? Do they sell churros?”

“CHOCOLAVA!!” bleeted the rabbit.

“What about those glow-in-the-dark ropes that you can put in your hair? Or you can just hold them by your side or up in the air—”

“BRAIDQUEST!!”

“You should get a cut of the salesman’s commission,” spoke Fox Small. “Folks came out to see you kill the dragon, right?”

“BUT!! I don’t operate the tongs that actually extract the chocolava.”

“I’m just sayin. You do operate the killing mechanism. So you have a stake in the ensalada.”

“OH NO!! I left my favorite lettuce leaves in Dwemthy’s Array!!” squealed the rabbit, twirling like a celebratory saber through the quaking oak. Distantly: “Or Lester’s trunk, maybe?”

“You know—Gheesh, can you stay put??” said Fox Small.

“My radio,” said Fox Tall, stirring to life for a moment, “in my pickup.” The glaze still seeping from his eyes. His stare quivered and set back into his face, recalling another time and place. A drive out to Maryland. Sounds of Lionel Richie coming in so clearly. The wipers going a bit too fast. He pulls up to a house. His mother answers the door. She is a heavily fluffed fox. Tears and makeup.

Slumping back down, “That porcupine is changing my presets.”

The rabbit bounded up on to the armrest of the park bench and spoke closely. “BUT!! Soon I will feast on drgn’s head and the juices of drgn’s tongue!!” The rabbit sat still and held his paws kindly.

“(Which I hope will taste like cinnamon bears,)” whispered the rabbit, intimately.

“I love cinammon,” said Fox Small. “I should go killing with you some time.”

“You should,” said the rabbit and the eyes shine-shined.

“Although, salivating over a tongue. You don’t salivate over it, do you?”

“I DO!!” and the rabbit got so excited that Sticky Whip shot out of his eyes. (More on Sticky Whip in a later sidebar. Don’t let me forget. See also: The Purist’s Compendium to Novelty Retinal Cremes by Jory Patrick Sobgoblin, available wherever animal attachment clips are sold.)

“Okay, you’ve hooked me. I want to hear all about it,” Fox Small declared. “Please, talk freely about the chimbly. Oh, and Dwemthy. Who is he? What makes him tick? Then maybe, if I’m still around after that, you can tell me about what makes rabbits tick, and maybe you can hold our hands through this whole missing truck ordeal. I need consolation more than anything else. I could probably use religion right now. I could use your personal bravery and this sense of accomplishment you exude. Do you smoke a pipe? Could be a handy tool to coax along the pontification we must engage in.”

And the rabbit began expounding upon Dwemthy and the legend of Dwemthy and the ways of Dwemthy. As with most stories of Dwemthy, the rabbit’s tales were mostly embellishments. Smotchkkiss, there are delicacies which I alone must address.

Please, never ask who Dwemthy is. Obviously he is a mastermind and would never disclose his location or true identity. He has sired dynasties. He has set living ogres aflame. Horses everywhere smell him at all times. Most of all, he knows carnal pleasures. And to think that this…

This is his Array.

Dwemthy’s Array

Dwemthy's Array has charmed and tormented the village folk for centuries.

You stand at the entrance of Dwemthy’s Array. You are a rabbit who is about to die. And deep at the end of the Array:

 class Dragon < Creature
   life 1340     # tough scales
   strength 451  # bristling veins
   charisma 1020 # toothy smile
   weapon 939    # fire breath
 end

A scalding SEETHING LAVA infiltrates the cacauphonous ENGORGED MINESHAFTS deep within the ageless canopy of the DWEMTHY FOREST... chalky and nocturnal screams from the belly of the RAVENOUS WILD STORKUPINE... who eats wet goslings RIGHT AFTER they’ve had a few graham crackers and a midday nap… amidst starved hippos TECHNICALLY ORPHANED but truthfully sheltered by umbrellas owned jointly by car dealership conglomerates… beneath uncapped vials of mildly pulpy BLUE ELIXIR... which shall remain… heretofore… UNDISTURBEDDWEMTHY!!!

Luck and mother nature.

If you don’t understand Dwemthy’s Array, it is Dwemthy’s fault. He designed the game to complicate our lives and were it simpler, it would not be the awe-inspiring quest we’ve come to cherish in our arms this very hour.

Dwemthy’s Array has a winding history of great depth. It is not enough to simply say, “Dwemthy’s Array,” over and over and expect to build credentials from that act alone. Come with me, I can take you back a couple years, back to the sixties where it all started with metaprogramming and the dolphins.

You might be inclined to think that metaprogramming is another hacker word and was first overheard in private phone calls between fax machines. Honest to God, I am here to tell you that it is stranger than that. Metaprogramming began with taking drugs in the company of dolphins.

In the sixties, a prolific scientist named John C. Lilly began experimenting with his own senses, to uncover the workings of his body. I can relate to this. I do this frequently when I am standing in the middle of a road holding a pie or when I am hiding inside a cathedral. I pause to examine my self. This has proven to be nigh impossible. I have filled three ruled pages with algebraic notation, none of which has explained anything. The pie, incidentally, has been very easy to express mathematically.

But the scientist Lilly went about his experiments otherwise. He ingested LSD in the company of dolphins. Often in a dark, woeful isolation tank full of warm salt water. Pretty bleak. But it was science! (Lest you think him criminal: until 1966, LSD was supplied by Sandoz Laboratories to any interested scientists, free of charge.)

Drugs, dolphins and deprivation. Which led to Lilly’s foray into things meta. He wrote books on mental programming, comparing humans and computers. You may choose to ingest any substance you want during this next quote - most likely you’re reaching for the grain of salt - but I assure you that there’s no Grateful Dead show on the lawn and no ravers in the basement.

When one learns to learn, one is making models, using symbols, analogizing, making metaphors, in short, inventing and using language, mathematics, art, politics, business, etc. At the critical brain (cortex) size, languages and its consequences appear. To avoid the necessity of repeating learning to learn, symbols, metaphors, models each time, I symbolize the underlying idea in these operations as metaprogramming.

John C. Lilly, Programming and Metaprogramming in the Human Biocomputer, New York, 1972.

We learn. But first we learn to learn. We setup programming in our mind which is the pathway to further programming. (Lilly is largely talking about programming the brain and the nervous system, which he collectively called the biocomputer.)

Lilly’s metaprogramming was more about feeding yourself imagery, reinventing yourself, all that. This sort of thinking links directly to folks out there who dabble in shamanism, wave their hands over tarot cards and wake up early for karate class. I guess you could say metaprogramming is New Age, but it’s all settled down recently into a sleeping bag with plain old nerdiness. (If you got here from a Google search for “C++ Metaprogramming”, stick around, but I only ask that you burn those neural pathways that originally invoked the search. Many thanks.)

Meta itself is spoken of no differently in your author’s present day.

All sensuous response to reality is an interpretation of it. Beetles and monkeys clearly interpret their world, and act on the basis of what they see. Our physical senses are themselves organs of interpretation. What distinguishes us from our fellow animals is that we are able in turn to interpret these interpretations. In that sense, all human language is meta-language. It is a second-order reflection on the ‘language’ of our bodies—of our sensory apparatus.

Terry Eagleton, After Theory, London, 2003, ch. 3.

To that end, you could say programming itself is a meta-language. All code speaks the language of action, of a plan which hasn’t been played yet, but shortly will. Stage directions for the players inside your machine. I’ve waxed sentimental on this before.

But now we’re advancing our study, venturing into metaprogramming, but don’t sweat it, it’s still just the Ruby you’ve seen already, which is why Dwemthy feels no qualms thrusting it at you right away. Soon enough it will be as easy to spot as addition or subtraction. At first it may seem intensely bright, like you’ve stumbled across your first firefly, which has flown up in your face. Then, it becomes just a little bobbing light which makes living in Ohio so much nicer.

Metaprogramming is writing code which writes code. But not as M.C. Escher would sketch it. The program isn’t reaching back around and overwriting itself, nor is the program jumping onto your screen and wrenching the keyboard from your hands. No, it’s much smaller than that.

Let’s say it’s more like a little orange pill you won at the circus. When you suck on it, the coating wears away and behind your teeth hatches a massive, floppy sponge brontosaurus. He slides down your tongue and leaps free, frollicking over the pastures, yelping, “Papa!” And from then on, whenever he freaks out and attacks a van, well, that van is sparkling clean afterwards.

Now, let’s say someone else puts their little orange pill under the faucet. Not on their tongue, under the faucet. And this triggers a different catalysm, which births a set of wailing sponge sextuplets. Umbilical cords and everything. Still very handy for cleaning the van. But an altogether different kind of chamois. And, one day, these eight will stir Papa to tears when they perform the violin concerto of their lives.

Metaprogramming is packing code into pill-form, such that a slender drop of water could trigger it to expand. More importantly, you can control the pill’s reaction, so that a brontosaurus is produced, scaly and lumbering. Or septulets, CERTAINLY. Or seamstresses. Or cat brains. Or dragons.

 class Dragon < Creature
   life 1340     # tough scales
   strength 451  # bristling veins
   charisma 1020 # toothy smile
   weapon 939    # fire breath
 end

This is not metaprogramming yet. Only the pill. The product of metaprogramming. We are pausing, looking at the beast itself before descending beneath its flesh with a scalpel and microscope.

The Dragon is a class. You’ve seen that many times now. The Dragon is a descendant of the Creature class.

Now, eyes up. Look at me. The Creature class contains the metaprogramming code. You can write metaprogramming code which can be used everywhere, throughout Ruby, in Creature or Dragon, in String or in Object, anywhere. Our example here, since this is the most common form of meta-code, focuses on metaprogramming inside your own classes only.

Each of the Dragon’s traits are simply class methods. You could also write this as:

 class Dragon < Creature
   life( 1340 )     # tough scales
   strength( 451 )  # bristling veins
   charisma( 1020 ) # toothy smile
   weapon( 939 )    # fire breath
 end

Removing the parens removes clutter, so let’s leave them out. Only use parens when you are using several methods together and you want to be very clear.

Creature Code

Now, with a lateral slice across the diaphragm, we expose the innards of Creature. Save this code into a file called dwemthy.rb.

 # The guts of life force within Dwemthy's Array
 class Creature

   # Get a metaclass for this class
   def self.metaclass; class << self; self; end; end

   # Advanced metaprogramming code for nice, clean traits
   def self.traits( *arr )
     return @traits if arr.empty?

     # 1. Set up accessors for each variable
     attr_accessor *arr

     # 2. Add a new class method to for each trait.
     arr.each do |a|
       metaclass.instance_eval do
         define_method( a ) do |val|
           @traits ||= {}
           @traits[a] = val
         end
       end
     end

     # 3. For each monster, the `initialize' method
     #    should use the default number for each trait.
     class_eval do
       define_method( :initialize ) do
         self.class.traits.each do |k,v|
           instance_variable_set("@#{k}", v)
         end
       end
     end

   end

   # Creature attributes are read-only
   traits :life, :strength, :charisma, :weapon
 end

Focus on the closing lines of code, specifically the line where the traits are being set up. All of the code before that line sets up the traits class method. This bears resemblance to the basic lottery tickets from the chapter previous.

 class LotteryTicket
   attr_reader :picks, :purchased
 end

Both traits and attr_reader are simply class methods. When attr_reader is used in the LotteryTicket, metaprogramming kicks in behind the scenes and starts blowing up balloons, creating reader methods for the instance variables @picks and @purchased above.

The code for the traits method is the metaprogramming I’ve been alluding to. Comments in the code reveal the three stages the method goes through when adding traits.

  1. The list of traits is passed on to attr_accessor, which builds reader and writer code for instance variables. One for each trait.
  2. Class methods are added for each trait. (A life class method is added for a :life trait.) These class methods are used in the class definition just like you would use traits or attr_accessor. This way, you can specify the trait, along with the points given for a trait to a certain creature.
  3. Add an initialize method which sets up a new monster properly, grabbing the right points and POWER UP! POWER UP! the monster is alive!

The beauty of these three steps is that you’ve taught Ruby how to code monsters for you. So when Ruby gets to the traits:

 class Creature
   traits :life, :strength, :charisma, :weapon
 end

Ruby fills in the code behind the scenes and transplants a spiny green heart and jumpstarts the body with a pullcord. Ruby will use the metaprogramming from the Creature class and build out all the various methods, expanding the traits list like this:

 class Creature

   # 1. set up reader and writer methods
   attr_accessor :life, :strength, :charisma, :weapon

   # 2. add new class methods to use in creature
   def self.life( val )
     @traits ||= {}
     @traits['life'] = val
   end

   def self.strength( val )
     @traits ||= {}
     @traits['strength'] = val
   end

   def self.charisma( val )
     @traits ||= {}
     @traits['charisma'] = val
   end

   def self.weapon( val )
     @traits ||= {}
     @traits['weapon'] = val
   end

   # 3. initialize sets the default points for
   #    each trait
   def initialize
     self.class.traits.each do |k,v|
       instance_variable_set("@#{k}", v)
     end
   end

 end

Now, Ruby will gladly accept this six-line Dragon code, short enough to look nice when printed on playing cards:

 class Dragon < Creature
   life 1340     # tough scales
   strength 451  # bristling veins
   charisma 1020 # toothy smile
   weapon 939    # fire breath
 end

Eval, the Littlest Metaprogrammer

While the metaprogramming code above is just plain Ruby, it can be difficult to follow still. I totally understand if you’ve come to this point and your eyes are spinning in their sockets and your knees have locked up. The trickiest parts of the above are the lines which call the methods instance_eval and class_eval. Just rub some tiger balm on your joints while I talk about eval.

We’ve been talking about metaprogramming. Writing code which writes code. The eval method hangs out in this alley. The vagrant eval takes code you have stored up in a string and it executes that code.

 drgn = Dragon.new
 # is identical to...
 drgn = eval( "Dragon.new" )
 # or, alternatively...
 eval( "drgn = Dragon.new" )

Here, let’s write a program which has a hole in it. Instead of writing a program which creates a new Dragon, let’s leave a hole where the Dragon would be.

 print "What monster class have you come to battle? " 
 monster_class = gets
 eval( "monster = " + monster_class + ".new" )
 p monster

The program asks for a monster. If you type in Dragon, then the monster_class variable will contain the string "Dragon". Inside the eval, a few strings get added together to make the string "monster = Dragon.new". And when the eval executes this string, the monster variable contains a Dragon object. Ready for battle.

This is great! Now we can leave it up to the player to pick a monster! Of course, we’re trusting the player to supply a real monster class. If they type in BotanicalWitch and there is no BotanicalWitch class, they’ll get an exception tossed in their face.

So, in short, eval lets you make up code as you go. Which can be useful and which can be dangerous as well.

The instance_eval and class_eval method used in the metaprogramming for the Creature class are slightly different from the normal eval. These two special methods run code just like eval does, but they duck into classes and objects and run the code there.

 # The instance_eval method runs code as if it were run inside an
 # object's instance method.
 irb> drgn = Dragon.new
 irb> drgn.instance_eval do
 irb>   @name = "Tobias" 
 irb> end

 irb> drgn.instance_variable_get( "@name" )
   => "Tobias" 

 # The class_eval method runs code is if inside a class definition.
 irb> Dragon.class_eval do
 irb>   def name; @name; end
 irb> end

 irb> drgn.name
   => "Tobias"

As you can see above, the instance_eval and class_eval methods also can take a code block instead of a string. Which is just how things are done in Dwemthy’s Array.

Enough Belittling Instruction and Sly Juxtaposition—Where Is Dwemthy’s Array??

Tread carefully—here is the other half of DWEMTHY’S ARRAY!! Add these lines to dwemthy.rb.

 class Creature

   # This method applies a hit taken during a fight.
   def hit( damage )
     p_up = rand( charisma )
     if p_up % 9 == 7
       @life += p_up / 4
       puts "[#{ self.class } magick powers up #{ p_up }!]" 
     end 
     @life -= damage
     puts "[#{ self.class } has died.]" if @life <= 0
   end

   # This method takes one turn in a fight.
   def fight( enemy, weapon )
     if life <= 0
       puts "[#{ self.class } is too dead to fight!]" 
       return
     end

     # Attack the opponent
     your_hit = rand( strength + weapon )
     puts "[You hit with #{ your_hit } points of damage!]" 
     enemy.hit( your_hit )

     # Retaliation
     p enemy
     if enemy.life > 0
       enemy_hit = rand( enemy.strength + enemy.weapon )
       puts "[Your enemy hit with #{ enemy_hit } points of damage!]" 
       self.hit( enemy_hit )
     end
   end

 end

 class DwemthysArray < Array
   alias _inspect inspect
   def inspect; "#<#{ self.class }#{ _inspect }>"; end
   def method_missing( meth, *args )
     answer = first.send( meth, *args )
     if first.life <= 0
       shift
       if empty?
         puts "[Whoa.  You decimated Dwemthy's Array!]" 
       else
         puts "[Get ready. #{ first.class } has emerged.]" 
       end
     end
     answer || 0
   end
 end

This code adds two methods to Creature. The hit method which reacts to a hit from another Creature. And the fight method which lets you place your own blows against that Creature.

When your Creature takes a hit, a bit of defense kicks in and your charisma value is used to generate a power-up. Don’t ask me to explain the secrets behind this phenomenon. A random number is picked, some simple math is done, and, if you’re lucky, you get a couple life points. @life += p_up / 4.

Then, the enemy’s blow is landed. @life -= damage. That’s how the Creature#hit method works.

The fight method checks to see if your Creature is alive. Next, a random hit is placed on your opponent. If your opponent lives through the hit, it gets a chance to strike back. Those are the workings of the Creature#fight method.

I’ll explain DwemthysArray in a second. I really will. I’m having fun doing it. Let’s stick with hitting and fighting for now.

Introducing: You.

You may certainly tinker with derivations on this rabbit. But official Dwemthy Paradigms explicitly denote the code - and the altogether character - inscribed below. Save this as rabbit.rb.

 class Rabbit < Creature
   traits :bombs

   life 10
   strength 2
   charisma 44
   weapon 4
   bombs 3

   # little boomerang
   def ^( enemy )
     fight( enemy, 13 )
   end
   # the hero's sword is unlimited!!
   def /( enemy )
     fight( enemy, rand( 4 + ( ( enemy.life % 10 ) ** 2 ) ) )
   end
   # lettuce will build your strength and extra ruffage
   # will fly in the face of your opponent!!
   def %( enemy )
     lettuce = rand( charisma )
     puts "[Healthy lettuce gives you #{ lettuce } life points!!]" 
     @life += lettuce
     fight( enemy, 0 )
   end
   # bombs, but you only have three!!
   def *( enemy )
     if @bombs.zero?
       puts "[UHN!! You're out of bombs!!]" 
       return
     end
     @bombs -= 1
     fight( enemy, 86 )
   end
 end

You have four weapons. The boomerang. The hero’s sword. The lettuce. And the bombs.

To start off, open up irb and load the libraries we’ve created above.

 irb> require 'dwemthy'
 irb> require 'rabbit'

Now, unroll yourself.

 irb> r = Rabbit.new
 irb> r.life
   => 10
 irb> r.strength
   => 2

Good, good.

Rabbit Fights ScubaArgentine!

You cannot just go rushing into Dwemthy’s Array, unseatbelted and merely perfumed!! You must advance deliberately through the demonic cotillion. Or south, through the thickets and labyrinth of coal.

For now, let’s lurk covertly through the milky residue alongside the aqueducts. And sneak up on the ScubaArgentine.

 class ScubaArgentine < Creature
   life 46
   strength 35
   charisma 91
   weapon 2
 end

To get the fight started, make sure you’ve created one of you and one of the ScubaArgentine.

 irb> r = Rabbit.new
 irb> s = ScubaArgentine.new

Now use the little boomerang!

 irb> r ^ s
 [You hit with 2 points of damage!]
 #<ScubaArgentine:0x808c864 @charisma=91, @strength=35, @life=44, @weapon=2>
 [Your enemy hit with 28 points of damage!]
 [Rabbit has died.]

For crying out loud!! Our sample rabbit died!!

Grim prospects. I can’t ask you to return to the rabbit kingdom, though. Just pretend like you didn’t die and make a whole new rabbit.

 irb> r = Rabbit.new

 # attacking with boomerang!
 irb> r ^ s

 # the hero's sword slashes!
 irb> r / s

 # eating lettuce gives you life!
 irb> r % s

 # you have three bombs!
 irb> r * s

Pretty neat looking, wouldn’t you say? The code in rabbit.rb alters a few math symbols which work only with the Rabbit. Ruby allows you to change the behavior of math operators. After all, math operators are just methods!

 # the boomerang is normally an XOR operator.
 irb> 1.^( 1 )
   => 0

 # the hero's sword normally divides numbers.
 irb> 10./( 2 )
   => 5

 # the lettuce gives the remainder of a division.
 irb> 10.%( 3 )
   => 1

 # the bomb is for multiplication.
 irb> 10.*( 3 )
   => 30

Where it makes sense, you may choose to use math operators on some of your Classes. Ruby uses these math operators on many of its own classes. Arrays, for example, have a handful of math operators which you can see in the list of instance methods when you type: ri Array.

 # the plus operator combines two arrays into a single array
 irb> ["D", "W", "E"] + ["M", "T", "H", "Y"]
   => ["D", "W", "E", "M", "T", "H", "Y"]

 # minus removes all items in the second array found in the first
 irb> ["D", "W", "E", "M", "T", "H", "Y"] - ["W", "T"]
   => ["D", "E", "M", "H", "Y"]

 # the multiplier repeats the elements of an array
 irb> ["D", "W"] * 3
   => ["D", "W", "D", "W", "D", "W"]

You may be wondering: what does this mean for math, though? What if I add the number three to an array? What if I add a string and a number? How is Ruby going to react?

Please remember these operators are just methods. But, since these operators aren’t readable words, it can be harder to tell what they do. Use ri. Often you’ll find that the operators are identical to other readable methods. You can then choose to use the operator or the method. Whichever is clearer to you.

 # divide with an operator method ...
 irb> 10 / 3
   => 3

 # ... or a readable method?
 irb> 10.div 3
   => 3

And that’s how the Rabbit’s sword divides.

The Harsh Realities of Dwemthy’s Array AWAIT YOU TO MASH YOU!!

Once you’re done playchoking the last guy with his oxygen tube, it’s time to enter The Array. I doubt you can do it. You left your hatchet at home. And I hope you didn’t use all your bombs on the easy guy.

You have six foes.

 class IndustrialRaverMonkey < Creature
   life 46
   strength 35
   charisma 91
   weapon 2
 end

 class DwarvenAngel < Creature
   life 540
   strength 6
   charisma 144
   weapon 50
 end

 class AssistantViceTentacleAndOmbudsman < Creature
   life 320
   strength 6
   charisma 144
   weapon 50
 end

 class TeethDeer < Creature
   life 655
   strength 192
   charisma 19
   weapon 109
 end

 class IntrepidDecomposedCyclist < Creature
   life 901
   strength 560
   charisma 422
   weapon 105
 end

 class Dragon < Creature
   life 1340     # tough scales
   strength 451  # bristling veins
   charisma 1020 # toothy smile
   weapon 939    # fire breath
 end

These are the living, breathing monstrosities of Dwemthy’s Array. I don’t know how they got there. No one knows. Actually, I’m guessing the IntrepidDecomposedCyclist rode his ten-speed. But the others: NO ONE knows.

If it’s really important for you to know, let’s just say the others were born there. Can we move on??

As Dwemthy’s Array gets deeper, the challenge becomes more difficult.

 dwary = DwemthysArray[IndustrialRaverMonkey.new,
                       DwarvenAngel.new,
                       AssistantViceTentacleAndOmbudsman.new,
                       TeethDeer.new,
                       IntrepidDecomposedCyclist.new,
                       Dragon.new]

Fight the Array and the monsters will appear as you go. Godspeed and may you return with harrowing tales and nary an angel talon piercing through your shoulder.

Start here:

 irb> r % dwary

Oh, and none of this “I’m too young to die” business. I’m sick of that crap. I’m not going to have you insulting our undead young people. They are our future. After our future is over, that is.

The rabbit has changed us.

The Making of Dwemthy’s Array

Fast forward to a time when the winds have calmed. The dragon is vanquished. The unwashed masses bow. We love you. We are loyal to you.

But what is this centipede nibbling in your eardrum? You dig with your finger, but you can’t get him out! Blasted! It’s that infernal Dwemthy’s Array again. Explain yourself Dwemthy!

Here, I shall unmask the Array itself for you.

 class DwemthysArray < Array
   alias _inspect inspect
   def inspect; "#<#{ self.class }#{ inspect }>"; end
   def method_missing( meth, *args )
     answer = first.send( meth, *args )
     if first.life <= 0
       shift
       if empty?
         puts "[Whoa.  You decimated Dwemthy's Array!]" 
       else
         puts "[Get ready. #{ first.class } has emerged.]" 
       end
     end
     answer || 0
   end
 end

By now, you’re probably feeling very familiar with inheritance. The DwemthysArray class inherits from Array and, thus, behaves just like one. For being such a mystery, it’s alarmingly brief, yeah?

So it’s an Array. Filled with monsters. But what does this extra code do?

Inspect

The inspect method isn’t really a necessary part of Dwemthy’s Array. It’s something Dwemthy added as a courtesy to his guests. (Many call him twisted, many call him austere, but we’d all be ignorant to go without admiring the footwork he puts in for us.)

Every object in Ruby has an inspect method. It is defined in the Object class, so it trickles down through the pedigree to every wee child object just born.

 irb> o = Object.new
   => #<Object:0x81d60c0>
 irb> o.inspect
   => "#<Object:0x81d60c0>"

Have you noticed this? Whenever we create an object in irb, this noisy #<Object> verbage stumbles out! It’s a little name badge for the object. The inspect method creates that name badge. The badge is just a string.

 irb> class Rabbit
 irb>   attr_accessor :slogan
 irb>   def initialize s; @slogan = s; end
 irb>   def inspect; "#<#{ self.class } says '#{ @slogan }'>"; end
 irb> end

 irb> class FakeRabbit < Rabbit
 irb> end

 irb> Rabbit.new "i blow'd the drgn's face off!!" 
   => #<Rabbit says 'i blow'd the drgn's face off!!'>
 irb> FakeRabbit.new "Thusly and thusly and thusly..." 
   => #<FakeRabbit says 'Thusly and thusly and thusly...'>

The thing is: irb is talking back. Every time you run some code in irb, the return value from that code is inspected. How handy. It’s a little conversation between you and irb. And irb is just reiterating what you’re saying so you can see it for your self.

You could write your own Ruby prompt very easily:

 loop do
   print ">> " 
   puts  "=> " + eval( gets ).inspect
 end

This prompt won’t let you write Ruby code longer than a single line. It’s the essence of interactive Ruby, though. How do you like that? Two of your recently learned concepts have come together in a most flavorful way. The eval takes the typed code and runs it. The response from eval is then inspected.

Now, as you are fighting monsters in irb, Dwemthy’s Array will be inspected and replying with the monsters you have left to fight.

The foxes eat out.

Method Missing

Don’t you hate it when you yell “Deirdre!” and like ten people answer? That never happens in Ruby. If you call the deirdre method, only one deirdre method answers. You can’t have two methods named the same. If you add a second deirdre method, the first one disappears.

You can, however, have a method which answers to many names.

 class NameCaller
   def method_missing( name, *args )
     puts "You're calling `" + name + "' and you say:" 
     args.each { |say| puts "  " + say }
     puts "But no one is there yet." 
   end
   def deirdre( *args )
     puts "Deirdre is right here and you say:" 
     args.each { |say| puts "  " + say }
     puts "And she loves every second of it." 
     puts "(I think she thinks you're poetic.)" 
 end

When you call the method deirdre above, I’m sure you know what will happen. Deirdre will love every second of it, you and your dazzling poetry.

But what if you call simon?

 irb> NameCaller.new.simon( 'Hello?', 'Hello? Simon?' )
 You're calling `simon' and you say:
   Hello?
   Hello? Simon?
 But no one is there yet.

Yes, method_missing is like an answering machine, which intercepts your method call. In Dwemthy’s Array we use call forwarding, so that when you attack the Array, it passes that attack on straight to the first monster in the Array.

 def method_missing( meth, *args )
   answer = first.send( meth, *args )
   # ... snipped code here ...
 end

See! See! That skinny little method_missing passes the buck!

4. So, Let's Be Clear: The Porcupine Is Now To The Sea

The porcupine and his kite.

5. Walking, Walking, Walking, Walking and So Forth

The evening grew dark around the pair of foxes. They had wound their way through alleys packed with singing possums, and streets where giraffes in rumpled sportscoats bumped past them with their briefcases. They kept walking.

And now the stores rolled shut their corrugated metal lids. Crickets crawled out from the gutters and nudged at the loose change.

Why such hard feelings?

“Anyway, you must admit he’s a terrible President,” said Fox Small. “Why does President Marcos have a rabbit as Vice President of the Foxes.”

“The Vice President? The rabbit with the eyebrows?”

“No, the rabbit with the huge sausage lips,” said Fox Small.

But their conversation was abruptly interupted by a freckly cat head which popped from the sky just above the sidewalk.

At least they're still in the book...

What is this about?!

Will the book finish?

Oh, come on. This is rich. More meta.

I’m not going to bother illustrating this discussion Blixy had with the foxes at this point! It’s all a bunch of conjecture. HOW can they presume to know the landscape of my family drama? I love my sister. For a long time, I worshipped her. (This is my sister Quil.)

I admit that there was a pretty painful day a few months ago and I kind of freaked out. I was laid out on the long patio chair by the pool in my mom’s backyard. I had a Dr. Pepper and a bit of german chocolate cake. I was eating with a kid fork. Everything else was in the dishwasher, that’s all they had. Three prongs.

My mom started talking about Quil. All about how much money she was blowing on pants and purses. A five-hundred dollar purse. And then she said, “She’s losing it. She sounded totally high on the phone.” (She nailed it on the head, Quil was smoking dope and loving it.)

So I’d been noticing how observant my mom could be. That’s why, when she said, “I actually think she’s on cocaine,” I physically stood up and chucked my soda across the yard.

It sailed off into the woods somewhere. We had been talking awhile, so it was dark when the can flew. I paced a bit. And then I screamed at the top of my lungs.

My uncle Mike was standing there with the glass door open, staring at me. He said something totally nervous like, “Oh, okay. Well, I’ll—” And the tea in his glass was swishing back and forth, sloshing all over. He disappeared. He’s not very good at saying things to people. He’s more of a whistler. And resonant.

Moving along.

So, to be completely honest, yes, I got a little mad. I got mad. You know. I dealt with it. Quil calls me regularly. For some stupid reason, I rarely call her.

Plus, she didn’t end up killing herself. So it’s just not an issue. Who knows if it was real. She just had a lot of vodka. And she’s little. So it was just scary to see Quil guzzling it down like that. I mean forcing it down.

But why talk about it? It’ll just make her feel like I’m disappointed. Or like I’m a jerk.

Well, I got off track there a bit. Where was I? Blix is basically helping the foxes around, getting them on the trail of their truck. Yeah, back to all that.

Frogs who save seats on the bus.

“We can’t squeeze on to this bus,” said the smallest fox.

“Guys, walk on up,” said Blix. “What’s the hold up? Oh, the frogs. Yeah, just squeeze through.” Blixy pushed from behind.

“Hey,” said the Tall Fox. “I’m crammed on this little step! Somebody move!”

“Did you get through—young fox??” said the cat.

“No,” said Fox Small, “can’t you see? The driver keeps shaking his head and it’s really making me nervous. I don’t think he wants us on.”

“Go,” said Blix. He stepped down from his step and walked around the bus, peering through the plexiglass windows. “Well, I don’t know, guys. I dunno. I guess it’s got a lot of frogs.” He pounded on the window. “Hey! Move over!”

And that’s the reality of riding intercity transit in Wixl. It’s terribly competitive. The morning bus is so crowded that most white collar animals get frogs to hold their seat through the nighttime. For whatever reason, it works. It’s become this staple of their workflow and their economy.

If you can muster up a bit of imagination, you can see a percent sign as a frog’s slanted face. Got the picture in your head? Now let me show you frogs that camp out inside strings.

 # The %s format is for placing full strings.
 irb> "Seats are taken by %s and %s." % ['a frog', 'a frog with teeth']
   => "Seats are taken by a frog and a frog with teeth." 

 # The %d format is for placing numbers, while the %f format is for
 # floats (decimal numbers).
 irb