We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e27ce5d commit d90f2bcCopy full SHA for d90f2bc
validate-ip-address.rb
@@ -0,0 +1,27 @@
1
+# frozen_string_literal: true
2
+
3
+# Runtime: 24 ms
4
+# Memory Usage: 9.6 MB
5
+def valid_ip_address(ip)
6
+ v4 = ip.split('.')
7
+ v6 = ip.split(':')
8
9
+ if v4.size == 4 && ip.count('.') == 3
10
+ if v4.all? { |d| d.to_i.to_s == d && d.to_i <= 255 && d.to_i >= 0 }
11
+ 'IPv4'
12
+ else
13
+ 'Neither'
14
+ end
15
+ elsif v6.size == 8 && ip.count(':') == 7
16
+ return 'Neither' unless v6.all? { |d| d.size <= 4 && !d.empty? }
17
18
+ v6 = v6.map { |d| d.sub(/^[0]+/, '') }.map { |d| d == '' ? '0' : d }
19
+ if v6.all? { |d| d.size <= 4 && d.to_i(16) >= 0 && d.upcase == d.to_i(16).to_s(16).upcase }
20
+ 'IPv6'
21
22
23
24
25
26
27
+end
0 commit comments