On Thursday 07 January 2010, Jagadeesh wrote:
> |On Jan 7, 3:44 pm, Stefano Crocco <stefano.cro... / alice.it> wrote:
> |> On Thursday 07 January 2010, Jagadeesh wrote:
> |> > |Here is how I am trying to do with array
> |> > |
> |> > |states = ['open', 'analyzed', 'feedback', 'closed']
> |> > |
> |> > | unless ( ( states.grep('closed').empty? ||       states.grep
> |> > |
> |> > |('feedback').empty? ) \
> |> > |
> |> > |       &&  (states.grep('open').empty? ||states.grep
> |> > |
> |> > |('analyzed').empty? ) )
> |> > |
> |> > |            puts "hello"
> |> > |   end
> |> > |
> |> > |Thanks
> |> 
> |> Still I don't understand exactly what you're trying to do. states
> |> contains all the four entries, so states.grep will never return an
> |> empty array and the puts will always be executed.
> |> 
> |> Please, can you explain in detail what you're trying to do? This way
> |> we'll be able to help you better.
> |> 
> |> Stefano
> |
> |Actually there may be an arry without all those values. So I extracted
> |keys into an array and was trying to grep in it.
> |
> |Thanks for your help.
> |
> |Thanks

You don't need to use grep to see whether an array contains an element. You 
can use include?:

if (states.include?('closed') || states.include?('feedback')) && 
(states.include?('open') || states.include?('analyzed'))

Another aopproach is to use the Array & operator, which gives the intersection 
between two arrays. You can do something like this:

first_group = ['open', 'analyzed']
second_group = ['closed', 'feedback']

if !(states & second_group).empty? && !(states & first_group).empty?

The reasoning is that, if states contains either the 'open' or the 'analyzed' 
entries, then its intersection with first_group (states & first_group) won't 
be empty. The same for the second group.

I hope this helps

Stefano