Ruby default class actions after require

132 views Asked by At

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?

1

There are 1 answers

1
matt On BEST ANSWER

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.