-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathIP4.rb
41 lines (35 loc) · 908 Bytes
/
IP4.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
=begin
Implement String#ipv4_address?, which should return true if given object is an
IPv4 address - four numbers (0-255) separated by dots.
It should only accept addresses in canonical representation, so no leading 0s,
spaces etc.
=end
# My Solution
class String
def ipv4_address?
result = 0
result += 10 if self[-1] =~ /[\D]/
self.split(".").each do |x|
result += 10 if x =~ /.*[\D].*/
result += 10 if x[0] == "0" && x[1] != nil
result += 10 if x == ""
if x.to_i >= 0 && x.to_i <= 255
result += 1
end
end
result == 4 ? true : false
end
end
# Better Solution
class String
def ipv4_address?
byte = /(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])/
!!(self =~ /\A#{byte}\.#{byte}\.#{byte}\.#{byte}\z/)
end
end
# Another Solution
class String
def ipv4_address?
/\A(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])(\.(?!$)|$)){4}\z/ === self
end
end