From: Aldric Giacomoni Date: 2009-11-24T01:52:14+09:00 Subject: Re: Best way to search through an array for an item matching a s Daniel Webb wrote: > am trying to find a nice easy/simple way to > - search through an array of strings match the string I've input > - use that index to print out the word before in the array. > > for example > [ahoy there,hello,me hearties,dear friends] > > 'hello' should output 'ahoy there' http://ruby-doc.org/core/classes/Array.html#M002178 def print_element_before_x_in_y x, y # Shut up. I am GOOD at method names. y.index x x.nil? ? nil : y[x-1] end This, minds you, wraps around, so if you find the first item in the array, it'll return y[-1] ... ;-) -- Posted via http://www.ruby-forum.com/.