Skip to content

Rack Walkthrough #9

@adamakhtar

Description

@adamakhtar

Hey @codereading/readers

as per the [discussion] https://github.com/codereading/HQ/issues/8 in HQ re: structure of codereading sessions, it seems a documented walkthrough of rack would be useful.

So perhaps we can walkthrough rack asking questions as we go along.

So to get thing rolling ill start.

Assuming we are using the rackup command to load an app

i.e.
rackup lobster.ru

then the first place to look would be bin/rackup.ru

where we discover it simply calls

Rack::Server.start

module Rack
  class Server

    # Start a new rack server (like running rackup). This will parse ARGV and
    # provide standard ARGV rackup options, defaulting to load 'config.ru'.
    #
    # Providing an options hash will prevent ARGV parsing and will not include
    # any default options.
    def self.start(options = nil)
      new(options).start
    end

    attr_writer :options

    def initialize(options = nil)
      @options = options
      @app = options[:app] if options && options[:app]
    end

    def options
      @options ||= parse_options(ARGV)
    end

  ....

end

As per the comments, if we dont pass an options hash to Server.start, Rack will parse ARGV for arguments (in this case we passed lobster.rb on the command line). The problem is I don't see how the code above would do this.

start calls new passing nil as the options argument. Within initialize nil is assigned to @options. Any reference to "options" will always refer to the argument so the method

def options
   @options ||= parse_options(ARGV)
end

will never get called. So how does rack call parse_options(ARGV) ???

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions