On Thursday 07 January 2010, Jagadeesh wrote:
> |hi Gurus,
> |
> |Here my data structure
> |
> |1234 => { 'open' => '1234-1',
> |               'analyzed' => '1234-2',
> |               'feedback' => '1234-3',
> |               'closed' => '1234-4'
> |             }
> |3455 => { 'open' => '3455-1',
> |               'analyzed' => '34552',
> |               'feedback' => '3455-3',
> |               'closed' => '3455-4'
> |             }
> |
> |My task is to display numbers [like 1234 or 3455] if they have ('open'
> |OR 'analyzed') AND ('closed' OR 'feedback')  keys. I tried following
> |code
> |
> |puts numbers if	$DATA[pr].has_key?( ( ('closed'	|| 'feedback')
> |                                                         && ('open' ||
> |'analyzed') ) )
> |
> |I am not sure what is wrong with it but its not testing these
> |conditions and printing all numbers. Please help me understanding
> |has_key method and writing correct condition.
> |
> |Thanks

Not tested, but I think the condition should be:

($DATA[pr].has_key?('closed') || $DATA[pr].has_key?('feedback')) &&
($DATA[pr].has_key?('open') || $DATA[pr].has_key?('analyzed'))

Your condition would always have translated to 

if $DATA[pr].has_key?('open')

This happens because ruby first evaluates the argument to has_key? and because 
of the way logical operators work. || returns the first operand which 
evaluates to true (that is, the first operand which is not false or nil). This 
means that:
'closed' || 'feedback' always gives 'closed'
'open' || 'analyzed' always gives 'open'

On the other hand, the && operator returns the last operand if both evaluate 
to true and the value of the one which evaluates to false if one evaluates to 
false (if both evaluate to false, it returns the first). In your case, you had 
the expression

'closed' && 'open'

which evaluates to 'open'.

I hope this helps

Stefano