From: Robert Dober Date: 2006-12-21T07:18:19+09:00 Subject: Re: Searching Stings with Arrays? ------=_Part_22344_4714896.1166653091087 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 12/20/06, dblack@wobblini.net wrote: > > Hi -- > > On Thu, 21 Dec 2006, Phil Cooperking wrote: > > > Hi, I've been having a little problem with this. most of my ruby is from > > rails and both are shaky. > > > > okay what I've got is a string (which is a browser user agent) and I > > would like to search that string with a predefined array of browsers. > > I've done this in php so I'm positve an easy solution exists in Ruby > > > > this is what I got > > @string = "mozilla/5.0 (macintosh; u; ppc mac os x; en) > > applewebkit/418.9.1 (khtml, like gecko) safari/419.3" > > > > array = ["shiira", "msie", "safari", "firefox", "netscape"] > > > > so I'd like to search the string for any matches, it should stop at > > safari and bob's sombodys uncle. > > > > this is what I've been trying to do. > > array.find {|b| b == @string}.to_s > > > > this does't work I know, but I'm I in the right direction? > > You could use: > > @string.include?(b) > > or, if you want to be more careful about false positives (like, if > "msie" was a substring in some other browser's string), you could do: > > array.find {|b| @string[/#{b}/] } > > which anchors to word boundaries. (All untested, so check for typos > and/or logic errors :-) Reminds me of Knuth's great saying Beware of this code, I have only proved it correct, I have never tried it! You'll be allright ;) David > > -- > Q. What's a good holiday present for the serious Rails developer? > A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) > aka The Ruby book for Rails developers! > Q. Where can I get Ruby/Rails on-site training, consulting, coaching? > A. Ruby Power and Light, LLC (http://www.rubypal.com) > > R -- "The real romance is out ahead and yet to come. The computer revolution hasn't started yet. Don't be misled by the enormous flow of money into bad defacto standards for unsophisticated buyers using poor adaptations of incomplete ideas." - Alan Kay ------=_Part_22344_4714896.1166653091087--