1

I remember that this problem came up before but I can't find the answer.

I require a file this way:

#lib/tm/agent/server.rb
require 'tm/agent/server'

And, without calling the Listen class explicitly, its initialize gets executed:

module Tm
  module Agent
    module Server

      require 'goliath'

      class Listen < Goliath::API
        def initialize
          puts "WHAT"
        end
        def response(env)
          [200, {}, "Hello World"]
        end
      end

    end #end Server
  end #end Agent
end #end Tm

How do I avoid initializing the class on require?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Istvan
  • 7,500
  • 9
  • 59
  • 109
  • Maybe this could help http://stackoverflow.com/questions/3745252/require-file-without-executing-code – halfelf Oct 17 '12 at 03:12

1 Answers1

2

This is due to a hook in the Goliath server that automatically starts a server when you run your script directly – it’s not a normal feature of Ruby.

To avoid it, don’t call require 'goliath', but use require 'goliath/api' instead.

matt
  • 78,533
  • 8
  • 163
  • 197