# File lib/liquid/context.rb, line 172
    def find_variable(key)

      # This was changed from find() to find_index() because this is a very hot
      # path and find_index() is optimized in MRI to reduce object allocation
      index = @scopes.find_index { |s| s.has_key?(key) }
      scope = @scopes[index] if index

      variable = nil

      if scope.nil?
        @environments.each do |e|
          variable = lookup_and_evaluate(e, key)
          unless variable.nil?
            scope = e
            break
          end
        end
      end

      scope     ||= @environments.last || @scopes.last
      variable  ||= lookup_and_evaluate(scope, key)

      variable = variable.to_liquid
      variable.context = self if variable.respond_to?(:context=)

      return variable
    end