title: Getting the server port outside a Controller
body: <p>I want to register a callback URL to my Rails server from a third-party application, and to perform this registering on startup so I can start catching notifications.</p>
  
  <p>Looking <a href="https://stackoverflow.com/questions/1554267/how-to-find-the-local-port-a-rails-instance-is-running-on">at this question and the answers</a> I see advice like:</p>
  
  <pre><code>class SomeController &lt; ApplicationController
    def some_action
      raise "I'm running on port #{request.port}."
    end
  end
  </code></pre>
  
  <p>This is no good as I am not in a Controller. Next there is:</p>
  
  <pre><code>Rails::Server.new.options[:Port]
  </code></pre>
  
  <p>However, I get this error from my code and <code>rails console</code>:</p>
  
  <pre><code>NoMethodError: undefined method 'Server' for Rails::Module
  </code></pre>
  
  <p>I've got the host name successfully using <code>Socket.gethostname</code>, but how do I get the port number?</p>