In Files

  • ipaddr.rb

Class/Module Index [+]

Quicksearch

IPSocket

Public Instance Methods

getaddress(s) click to toggle source
 
               # File ipaddr.rb, line 46
def getaddress(s)
  if valid?(s)
    s
  elsif /\A[-A-Za-z\d.]+\Z/ =~ s
    getaddress_orig(s)
  else
    raise ArgumentError, "invalid address"
  end
end
            
valid?(addr) click to toggle source
 
               # File ipaddr.rb, line 41
def valid?(addr)
  valid_v4?(addr) || valid_v6?(addr)
end
            
valid_v4?(addr) click to toggle source
 
               # File ipaddr.rb, line 21
def valid_v4?(addr)
  if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr
    return $~.captures.all? {|i| i.to_i < 256}
  end
  return false
end
            
valid_v6?(addr) click to toggle source
 
               # File ipaddr.rb, line 28
def valid_v6?(addr)
  # IPv6 (normal)
  return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr
  return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
  return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
  # IPv6 (IPv4 compat)
  return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && valid_v4?($')
  return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_v4?($')
  return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_v4?($')

  false
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus