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 Secret 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!!)

One possibility is to use ipaddr:

require 'ipaddr'

def valid_ip?(ip_addr)
  begin
    ip = IPAddr.new(ip_addr)
  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

Andreas Secret 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!!)

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 Cheung wrote:

Phillip Koebbe wrote: