Last active
August 30, 2016 15:19
-
-
Save kjlape/d3383d0cdd74bf3ee739af0be4151b00 to your computer and use it in GitHub Desktop.
Ruby hack to get an ephemeral localhost port from the system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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