Task/Parse-an-IP-Address/Ruby/parse-an-ip-address.rb
# Parse _string_ for an IP address and optional port number. Returns
# them in an Addrinfo object.
Parse _string_ for an IP address and optional port number. Returns
them in an Addrinfo object.
Parse _string_ for an IP address and optional port number . Returns them in an Addrinfo object .
def parse_addr(string)
  case string
  when /\A\[(?<address> .* )\]:(?<port> \d+ )\z/x      
    address, port = $~[:address], $~[:port]
  when /\A(?<address> [^:]+ ):(?<port> \d+ )\z/x       
    address, port = $~[:address], $~[:port]
  else                                                 
    address, port = string, nil
  end
  ary = Addrinfo.getaddrinfo(address, port)
  ary.size == 1 or raise SocketError, "expected 1 address, found #{ary.size}"
  ary.first
end
def parse_addr ( string ) case string when / \A \[ (?<address> .* ) \] :(?<port>  \d + ) \z /x address , port = $~ [ :address ] , $~ [ :port ] when / \A (?<address> [^:]+ ):(?<port>  \d + ) \z /x address , port = $~ [ :address ] , $~ [ :port ] else address , port = string , nil end ary = Addrinfo . getaddrinfo ( address , port ) ary . size == 1 or raise SocketError , "expected 1 address, found #{ary.size}" ary . first end
Parse _string_ for an IP address and optional port number.
Parse _string_ for an IP address and optional port number .
# Split host and port number from string. # string like "[::1]:80" # string like "127.0.0.1:80" # string with no port number # Pass address, port to Addrinfo.getaddrinfo. It will raise SocketError if address or port is not valid. # IPAddr currently cannot handle ::1 notation, use Addrinfo instead # An IP address is exactly one address.
string
<not_specific>