tags:

views:

41

answers:

2

In the first method listed below, the use method, it looks to me like :ins is an instance variable and attr is a method that provides getters and setters for :ins. What I'm not sure is what the @ins << lambda does.

module Rack
  class Builder
    attr :ins
    def use(middleware, *args, &block)
      middleware.instance_variable_set "@rack_builder", self
      def middleware.rack_builder
        @rack_builder
      end
      @ins << lambda { |app|
        middleware.new(app, *args, &block)
      }
    end

    def run(app)
      klass = app.class
      klass.instance_variable_set "@rack_builder", self
      def klass.rack_builder
        @rack_builder
      end
      @ins << app #lambda { |nothing| app }
    end

    def leaf_app
      ins.last
    end
  end
end
+2  A: 

<< is the array push operator. So this is pushing a lambda onto an array (or at least something array-like) called @ins.

Chuck
+5  A: 

@ins is an instance variable which contains an Array. Arrays support the operator <<, which appends an item to the end of the array. For example: [1, 2] << 3 results in [1, 2, 3]. lambda is a method which creates a Proc.

So, to summarize: this code takes a block, creates a Proc out of it, and appends it to @ins.

Pesto