Ruby Behaviors

August 16, 2001

Ruby Behaviors, v. 0.0.2 (alpha), is now available for download.

Changes and enhancements include:

Feedback is always welcome; send email to me, David Alan Black.


[Note: As an introduction to Ruby Behaviors, here is the increasingly out-of-date, but still sort of philosophically relevant, thing I posted before there were any actual releases.]

What follows is a top-level, somewhat wishful description of something I am working on. Updates will be forthcoming. Feedback is welcome.

Your host,

David Alan Black

Introduction

Ruby Behaviors are locally-scoped changes to the Ruby core language. They are managed through facilities provided by class Behavior.

The goal of Ruby Behaviors is to make it very easy to make temporary changes to Ruby, without interfering with other code and without having to convince anyone else of the merits of the change. This will, I hope, take the burden off the RCR process, and open up a way for people with similar tastes in Ruby tweaking to share modifications to their programming environments.

Sketch of the Ruby Behaviors system

The overall structure will be: Also, there will be facilities for sharing and centralizing Behaviors.

Examples of using Ruby Behaviors

For these examples, we assume that a set of Behaviors called "DavidsWay" has been created and made accessible to the Behavior management facilities of class Behavior. "DavidsWay" adds a #to_h method to Enumerable, and a #byte_sort method to String.

   # Style 1 (explicit adopt/desist)
         require behavior
         b = Behavior.new(:DavidsWay)

         b.adopt

         a = [1,2,3,4]
         h = a.to_h(true)   # => { 1 => true, 2 => true, ... }

         b.desist

   # Style 2 (block)
         require behavior
         b = Behavior.new(:DavidsWay)

         b.adopt do
           puts "hello".byte_sort    # => ehllo
         end

Writing a Behavior

The process of writing a new Ruby Behavior will be very close to simply writing or rewriting one or more methods for one or more core class or module. It will also have hooks into the Behavior system. The form of these is yet to be determined, but roughly speaking there will be "start" and "stop" hooks.

The problem area addressed by class Behavior

(adapted from ruby-talk:14556)

We've got two basic mechanisms for publically extending Ruby: language change (RCR) and module/software archive (RAA). However, it seems to me that Ruby happens to excel in generating, or inspiring, a type of code which unfortunately falls right into the void between RCR and RAA.

I'm thinking of things like the by-products of RCR discussions. Say we talk about Array#insert, or Array#to_h, or whatever. Often, several suggested versions are perfectly respectable and useful in their own way. Of course if the method is going to be part of the language then one version has to prevail. But then the other ideas more or less vanish, at least from the communal radar.

The language-change/RCR process inevitably (and understandably) has this winner-take-all aspect to it. On the other side -- as a possible first answer to the question of what to do with miscellaneous good code -- is RAA. But the problem is that RAA lies too far in the other direction. A five-line Array#insert implementation isn't going to end up on RAA.

I wonder whether there's some more systematic way to leverage the (to me) sometimes startlingly powerful little language enhancements and extensions that get thrown around.

Miscellaneous comments


Copyright (c) David Alan Black, 2001.