Skip to content

Instantly share code, notes, and snippets.

@kjlape
Last active August 30, 2016 15:19
Show Gist options
  • Select an option

  • Save kjlape/d3383d0cdd74bf3ee739af0be4151b00 to your computer and use it in GitHub Desktop.

Select an option

Save kjlape/d3383d0cdd74bf3ee739af0be4151b00 to your computer and use it in GitHub Desktop.
Ruby hack to get an ephemeral localhost port from the system.
def ephemeral_port
unless @ephemeral_port
tcp_server = TCPServer.new('localhost', 0)
@ephemeral_port ||= tcp_server.addr[1]
tcp_server.close
end
@ephemeral_port
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment