On Tue, Nov 11, 2008 at 4:47 PM, Todd Benson <caduceass / gmail.com> wrote:
> On Tue, Nov 11, 2008 at 4:29 PM, Stuart Clarke
> <stuart.clarke1986 / gmail.com> wrote:
>> Hi all,
>>
>> I have a quick brain teaser. I  have an array full of data broken down
>> into indivdual entries. I running a simple if statement to check for an
>> entry in the array, if my if statement finds that entry its prints out
>> that entry. I want to add an extra step into this so, when my if
>> statement finds an entry, I want it to look at the next entry and check
>> for something else, if it finds that, I want to look at the next entry
>> and check for something else, if it finds all of the three entries in a
>> row, it prints out "You have found what you want"
>>
>> Any ideas?
>
> Depends on the objects in the array, but you might try #join followed
> by #include?.

That may have been a bit unfair (and wrong too, because you might have
nils in your array and temporarily destroys objects you don't expect).

How about (doesn't work if you have trailing nils in your search array)...

#this is just preemptive room cleaning, important code is further below
m = my_array.dup.unshift(Object.new) #big array
g = good_array #stuff I want to look for

#take one off the beginning until I match what I'm looking for
m.shift until (m[0..2] == g || m.empty?)
puts "Great!" if !m.empty?


...which makes a lot of english sense to me.  The my_array is #dup'ed
because #shift is destructive in pulling each thing from the
beginning.  g is just there to be concise.

You can condense this, too, if you wanted.  I don't know if you need
the #unshift or not.

cheers,
Todd