# Simple split function gives wrong output

I want to make a simple IP check for values between 0-255 without using
expressions, so I created the code below: (But c and d always gives
first if -statement. Does not not split handle 4 integer split, or have
I done it incorrectly?)

(a,b,c,d) = ARGV[0].split(’.’)

if (a < ‘0’ || a > ‘255’ || b < ‘0’ || b > ‘255’ || c < ‘0’ || c > ‘255’
|| d < ‘0’ || d > ‘255’)
puts " IP IS WRONG"
else
puts “IP is good”
end

Andreas S. wrote:

I want to make a simple IP check for values between 0-255 without using
expressions, so I created the code below: (But c and d always gives
first if -statement. Does not not split handle 4 integer split, or have
I done it incorrectly?)

(a,b,c,d) = ARGV[0].split(’.’)

if (a < ‘0’ || a > ‘255’ || b < ‘0’ || b > ‘255’ || c < ‘0’ || c > ‘255’
|| d < ‘0’ || d > ‘255’)
puts " IP IS WRONG"
else
puts “IP is good”
end

Forexample try with IP: 128.141.58.42 (Which is correct!!)

On Jan 13, 2008, at 9:04 AM, Andreas S. wrote:

## Forexample try with IP: 128.141.58.42 (Which is correct!!)

One possibility is to use ipaddr:

begin
rescue
# can specify ArgumentError if you want
ip = nil
end

return ip && ip.ipv4?
end

That’s what I do in one application.

Peace,
Phillip

On 13 Jan 2008, at 15:04, Andreas S. wrote:

Forexample try with IP: 128.141.58.42 (Which is correct!!)
You are comparing as strings, and in the lexicographical ordering ‘58’
is > ‘255’ since ‘5’ > ‘2’.
Convert to integers before comparing and you should be ok.

Fred

Frederick C. wrote:

On 13 Jan 2008, at 15:04, Andreas S. wrote:

Forexample try with IP: 128.141.58.42 (Which is correct!!)
You are comparing as strings, and in the lexicographical ordering ‘58’
is > ‘255’ since ‘5’ > ‘2’.
Convert to integers before comparing and you should be ok.

Fred

Fred,

thank you very much for that enlightning information, certainly found my
mistake!

Phillip K. wrote:

On Jan 13, 2008, at 9:04 AM, Andreas S. wrote:

## Forexample try with IP: 128.141.58.42 (Which is correct!!)

One possibility is to use ipaddr:

begin
rescue
# can specify ArgumentError if you want
ip = nil
end

return ip && ip.ipv4?
end

That’s what I do in one application.

Peace,
Phillip

Philip,

good idea for checking IP’s I’ll explore it a little further