Bits of code

NSObject+ProcObservation

NSObject+ProcObservation is a small reworking of AndyMatuschak's NSObject+BlockObservation to handle Proc objects from MacRuby. Tested with MacRuby 0.5b2.

Here's an example for registering undo:

anObject.observe('name', NSKeyValueObservingOptionOld) do |obj, change|
  undoManager.prepare(obj).setName(change[NSKeyValueChangeOldKey])
end
      

Like the original, #observe returns a token that can later be used to unregister observations. In addition to the original, the ability to register for a single observation (i.e. unregistration immediately after the first observation) is borrowed from Jens Alfke's MYUtilities.

Beats the pants off of -addObserver:forKeyPath:options:context: and -observeValueForKeyPath:ofObject:change:context: and contexts and all that ugly stuff. Keeps the observation code nearby too.