# Simple split function gives wrong output

**URL:** <https://www.ruby-forum.com/t/simple-split-function-gives-wrong-output/125902>\
**Category:** Rails\
**Created:** [January 13, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/simple-split-function-gives-wrong-output/125902 "2008-01-13T15:58:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [January 13, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/simple-split-function-gives-wrong-output/125902/1 "2008-01-13T15:58:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [January 13, 2008, 4:04pm UTC](https://www.ruby-forum.com/t/simple-split-function-gives-wrong-output/125902/2 "2008-01-13T16:04:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [January 13, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/simple-split-function-gives-wrong-output/125902/3 "2008-01-13T16:46:25Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [January 13, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/simple-split-function-gives-wrong-output/125902/4 "2008-01-13T17:13:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [January 13, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/simple-split-function-gives-wrong-output/125902/5 "2008-01-13T17:33:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![andreas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@andreas](https://www.ruby-forum.com/u/andreas)\
**Post date:** [January 13, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/simple-split-function-gives-wrong-output/125902/6 "2008-01-13T17:34:13Z")

</div>

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

Philip,

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