Source Code / Report Issue / API <img src=“https://github.com/rubyworks/cuts/actions/workflows/ci.yml/badge.svg” alt=“CI” />
Cuts is a pure-Ruby implementation of cut-based AOP (Aspect Oriented Programming). A cut is a transparent subclass: it sits in front of a class, its methods are called first, and they reach the class’s own with super.
Cuts are where Ruby’s Module#prepend came from, and since version 2.0 of this library a cut is built on it. See “What Became of Cuts”, below.
Cuts started its life as a discussion about AOP on Ruby-talk, which led to an in-depth discussion between Trans and Peter Vanbroekhoven. The outcome of those talks were three projects: Suby, an early experimental branch of Ruby; the RCRFoundry, a section of the Ruby Garden Wiki dedicated to jointly developing RCRs; and the Cut-based AOP RCR. Neither Suby nor the RCRFoundry exists any more. The RCR does, in this repository. It ultimately led to this project, as well as to an experimental Ruby 1.8.3 core implementation, which was part of Suby.
Please read the Cut-based AOP for Ruby RCR.
This Cuts library comes by way of Ruby Facets, where the implementation was originally housed. Because of its experimental nature, it was deemed best to move it into its own project as part of the work to make Facets a rock solid library.
Cuts were designed by Trans and Peter Vanbroekhoven as a general scheme for AOP, one that could be fitted to more or less any object-oriented language, and were proposed for Ruby in the RCR.
Matz’s reply, at the time, was to suggest something smaller: a method, prepend, that would put a module in front of a class rather than behind it. He asked whether that would be sufficient. We argued for the whole design, with the cut as a kind of class in its own right. (That may have been a mistake, in that it may have held prepend up a little.)
Matz was right. prepend was enough for Ruby. Module#prepend arrived in Ruby 2.0, in 2013, and with it a module can do what a cut was designed to do: wrap the methods of a class, transparently, and call them with super.
So this library is kept as the record of that idea, and as a working one. Until version 2.0 it had to imitate a cut, by extending each object as it was made. Now a cut simply is a module prepended to its class, and what the library adds is what prepend left out: the cut as a declared and named thing, which knows its class and which its class can list.
Here is a quick and dirty example:
require 'cuts' class C def f ; "f" ; end end cut :G < C do def f; '<'+super+'>' ; end end c = C.new c.f #=> "<f>"
A cut knows its class, and a class its cuts.
G.base #=> C C.cuts #=> [G]
Which is the same as, and is built on:
module G def f; '<'+super+'>' ; end end class C prepend G end
On top of cuts is a small AOP system, of aspects, advice and join-points.
require 'cuts/aop' Bracket = Aspect.new do join :bracket do |joinpoint| joinpoint == :f end def bracket(target) '[' + target.super + ']' end end C.apply(Bracket) c.f #=> "[<f>]"
The demonstrations have more, and are run as tests with QED. For the methods, see the API Documentation.
Install via RubyGems:
$ gem install cuts
Cuts needs Ruby 3.1 or newer.
Special thanks to Peter Vanbroekhoven. The man is a genius!
Cuts, Copyright © 2007 Thomas Sawyer & Peter Vanbroekhoven
Cuts is distributable in accordance with the FreeBSD License.
See NOTICE.rdoc for details.