[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:02308] Re: DBC

From: matz@... (Yukihiro Matsumoto)
Date: 2000-03-31 18:22:19 UTC
List: ruby-talk #2308
Hi,

In message "[ruby-talk:02298] Re: DBC"
    on 00/03/31, Andrew Hunt <andy@Toolshed.Com> writes:

|My Rube-Goldberg attempt used tracefunc to hook into the dispatcher,
|so that I could check every method call and see if a pre/post condition
|existed and then call it.  This also gave me the opportunity to call
|invariant on exit from every method call.

The tracefunc may be a performance killer.  I'm not sure whether using
reflection is better or not.  Here's the basic concept.

  module DBC
    def DBC::assertion(mod)
      methods = mod.instance_methods
      methods.each do |m|
        next if m =~ /__(pre|post|orig)$/
        pre = m+"__pre"
        post = m+"__post"
        if methods.include?(pre) and methods.include?(post)
          mod.module_eval %{
            alias #{m}__orig #{m}
            def #{m}(*args, &block)
              #{m}__pre(*args)
              result = #{m}__orig(*args, &block)
              #{m}__post(result, *args)
              result
            end
          }
        elsif methods.include?(pre)
          mod.module_eval %{
            alias #{m}__orig #{m}
            def #{m}(*args, &block)
              #{m}__pre(*args)
              #{m}__orig(*args, &block)
            end
          }
        elsif methods.include?(post)
          mod.module_eval %{
            alias #{m}__orig #{m}
            def #{m}(*args, &block)
              result = #{m}__orig(*args, &block)
              #{m}__post(result, *args)
              result
            end
          }
        end
      end
    end
  end

  class Foo
    include DBC
    def foo__pre
      p "pre"
    end
    def foo__post(result)
      p "post"
    end
    def foo
      p "foo"
    end
    DBC::assertion(self)
  end

  foo = Foo.new
  foo.foo

By the way, what DBC stand for?

							matz.

In This Thread