Jan 062012
 

Ruby has a neat feature called at_exit which takes a block and then executes the contents of this block when the program ends. There are a couple of VERY important details:

  1. It takes a block and converts it into a Proc object at the time of parsing. This means that the data has to be available in the binding, or you’ll run into errors. Example: instance variables need to be set before you can use them in that block. Better idea: don’t use instance variables in there at all.
  2. You can ‘chain’ at_exit calls, and they will be resolved in a First In, Last Out (FILO) order.

Once you know this, using at_exit and writing tests for it becomes a little easier:

class Piddler
   def initialize
     create_pid_file
   end

   private

   def create_pid_file
     pid_file = "/tmp/piddler/my_pid" #Simplified for example purposes
     File.new(pid_file, 'w')
     at_exit { FileUtils.rm_f pid_file }
   end
end

What you’ll notice is that the at_exit block is defined RIGHT AFTER I create what I will need to resolve/undo/finish – not separately, right inside the method.

 

def test_clears_pid_file_when_it_exits
   at_exit { assert_equal 0, Dir['/tmp/piddler/*].size}
   Piddler.new
end

The advantage of that is that I know exactly when it gets defined. For this example, it gets defined at the end of the ‘initialize > create_pid_file’ call. This means that any at_exit blocks defined BEFORE that will be resolved AFTER.

 

Dec 192008
 

This week two more students tested. We have a new green belt and a new blue belt. They did pretty well, but I am completely amazed at how many excuses adults have. Their heads are full of them. Can’t they just listen and say ‘yes’ like they were taught when they were kids? Talking back is a privilege, not a right.
Ooh.. That’s good.. I’ll reuse that.

May 012008
 

Monday and yesterday, two blue belts were to test for purple. They went through their calisthenics and basics; I did not see their forms on Monday, but I did see them on Wednesday. They were bad enough sensei wanted to see them again, that much I understood, but not sure how bad until I saw them both.

Neither of them broke a sweat until fighting, not for the 100 jumping jacks, not for the 60 push-ups, not for the multiple kick combinations. They did get out of breath when doing all the forms in a row, at least…
Neither displayed the spirit we expected from a blue belt.. Let alone from someone testing. A test is supposed to be the hardest thing you’ve done to date and you’re supposed to work as hard as you can.. And that’s usually noticeable. Further, their forms weren’t even up to blue belt level. Chances are they did not practice outside the dojo.. Or even think about the art at all. While some people do martial arts leisurely, you should either expect to stop progressing or realize that work needs to go into it.

One of the blue belts seemed OK with the postponement and stayed to speak with sensei.. The other one pretty much stormed out.. Well, we’ll see what happens now.
You have to TRAIN !

%d bloggers like this: