def wrap_method(klass)
      check_name = "__protocol_check_#{name}"
      if klass.method_defined?(check_name)
        inner_name = "__protocol_inner_#{name}"
        unless klass.method_defined?(inner_name)
          args =
            if arity >= 0
              (1..arity).map { |i| "x#{i}," }
            else
              (1..~arity).map { |i| "x#{i}," } << '*rest,'
            end.join
          wrapped_call = %{
            alias_method :'#{inner_name}', :'#{name}'

            def precondition
              yield or
                raise Protocol::PreconditionCheckError.new(
                  ObjectSpace._id2ref(#{__id__}),
                  "precondition failed for \#{self.class}")
            end unless method_defined?(:precondition)

            def postcondition(&block)
              post_name = "__protocol_#{klass.__id__.abs}_postcondition__"
              (Thread.current[post_name][-1] ||= Protocol::Postcondition.new(
                self)).__add__ block
            end unless method_defined?(:postcondition)

            def #{name}(#{args} &block)
              result = nil
              post_name = "__protocol_#{klass.__id__.abs}_postcondition__"
              (Thread.current[post_name] ||= MyArray.new) << nil
              __send__('#{check_name}', #{args} &block)
              if postcondition = Thread.current[post_name].last
                begin
                  reraised = false
                  result = __send__('#{inner_name}', #{args} &block)
                  postcondition.__result__= result
                rescue Protocol::PostconditionCheckError => e
                  reraised = true
                  raise e
                ensure
                  unless reraised
                    postcondition.__check__ or
                      raise Protocol::PostconditionCheckError.new(
                        ObjectSpace._id2ref(#{__id__}),
                        "postcondition failed for \#{self.class}, result = " +
                        result.inspect)
                  end
                end
              else
                result = __send__('#{inner_name}', #{args} &block)
              end
              result
            rescue Protocol::CheckError => e
              case ObjectSpace._id2ref(#{__id__}).protocol.mode
              when :error
                raise e
              when :warning
                warn e
              end
            ensure
              Thread.current[post_name].pop
              Thread.current[post_name].empty? and
                Thread.current[post_name] = nil
            end
          }
          klass.class_eval wrapped_call
        end
      end
    end
def wrap_method(klass)
      check_name = "__protocol_check_#{name}"
      if klass.method_defined?(check_name)
        inner_name = "__protocol_inner_#{name}"
        unless klass.method_defined?(inner_name)
          args =
            if arity >= 0
              (1..arity).map { |i| "x#{i}," }
            else
              (1..~arity).map { |i| "x#{i}," } << '*rest,'
            end.join
          wrapped_call = %{
            alias_method :'#{inner_name}', :'#{name}'

            def precondition
              yield or
                raise Protocol::PreconditionCheckError.new(
                  ObjectSpace._id2ref(#{__id__}),
                  "precondition failed for \#{self.class}")
            end unless method_defined?(:precondition)

            def postcondition(&block)
              post_name = "__protocol_#{klass.__id__.abs}_postcondition__"
              (Thread.current[post_name][-1] ||= Protocol::Postcondition.new(
                self)).__add__ block
            end unless method_defined?(:postcondition)

            def #{name}(#{args} &block)
              result = nil
              post_name = "__protocol_#{klass.__id__.abs}_postcondition__"
              (Thread.current[post_name] ||= MyArray.new) << nil
              __send__('#{check_name}', #{args} &block)
              if postcondition = Thread.current[post_name].last
                begin
                  reraised = false
                  result = __send__('#{inner_name}', #{args} &block)
                  postcondition.__result__= result
                rescue Protocol::PostconditionCheckError => e
                  reraised = true
                  raise e
                ensure
                  unless reraised
                    postcondition.__check__ or
                      raise Protocol::PostconditionCheckError.new(
                        ObjectSpace._id2ref(#{__id__}),
                        "postcondition failed for \#{self.class}, result = " +
                        result.inspect)
                  end
                end
              else
                result = __send__('#{inner_name}', #{args} &block)
              end
              result
            rescue Protocol::CheckError => e
              case ObjectSpace._id2ref(#{__id__}).protocol.mode
              when :error
                raise e
              when :warning
                warn e
              end
            ensure
              Thread.current[post_name].pop
              Thread.current[post_name].empty? and
                Thread.current[post_name] = nil
            end
          }
          klass.class_eval wrapped_call
        end
      end
    end
Hack to make checking against Array possible.
 :startdoc: