From: Robert Klemme Date: 2011-02-07T23:54:24+09:00 Subject: Re: Find in Array 2011/2/7 Jesús Gabriel y Galán : > On Mon, Feb 7, 2011 at 3:22 PM, Benjamin S. > wrote: >> As I see it, with these methods i can only search for full strings. My >> problem is, that i get a string and my search parameter is only a part >> of this string (so find, include, find_index etc doesn't work...) > > You can use find/select with a regular expression: > > > irb(main):037:0> a = %w{test test2 nothing something} > => ["test", "test2", "nothing", "something"] > irb(main):038:0> a.find {|x| /test/ =~ x} > => "test" > irb(main):039:0> a.select {|x| /test/ =~ x} > => ["test", "test2"] > irb(main):040:0> a.select {|x| /thing/ =~ x} > => ["nothing", "something"] The last one is even simpler with #grep: irb(main):001:0> a = %w{test test2 nothing something} => ["test", "test2", "nothing", "something"] irb(main):002:0> a.grep /thing/ => ["nothing", "something"] irb(main):003:0> Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/