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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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" > endForexample try with IP: 128.141.58.42 (Which is correct!!) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 13, 2008, at 9:04 AM, Andreas Secret wrote:> > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 13 Jan 2008, at 15:04, Andreas Secret wrote:> > 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> > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 13 Jan 2008, at 15:04, Andreas Secret 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. > > FredFred, thank you very much for that enlightning information, certainly found my mistake! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe wrote:> On Jan 13, 2008, at 9:04 AM, Andreas Secret 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, > PhillipPhilip, good idea for checking IP''s :) I''ll explore it a little further -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---