On Thu, Jan 7, 2010 at 10:55 AM, Jagadeesh <mnjagadeesh / gmail.com> wrote:
> On Jan 7, 2:33  夨ı  <jgabrielyga... / gmail.com>
> wrote:
>> On Thu, Jan 7, 2010 at 10:20 AM, Jagadeesh <mnjagade... / gmail.com> 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') eys. 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.
>>
>> The has_key? method receives a single key to be checked in the hash.
>> In your snippet, what you are passing to the has_key? method is the
>> result of evaluating this:
>>
>> ( ('closed' || 'feedback') && ('open' || 'analyzed') )
>>
>> which is:
>>
>> irb(main):002:0> ( ('closed' || 'feedback') && ('open' || 'analyzed') )
>> => "open"
>>
>> So you are testing only for the "open" key. I don't know what $DATA or
>> pr or numbers are in your snippet, but you are testing only for the
>> key "open".
>>
>> Hope this helps,
>>
>> Jesus.
>
> $DATA=>{'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'
> 
> }
>
> and am reading it in loop so pr will have 1234, 3455 etc.

As Stefano said:

irb(main):001:0> $DATA = {
irb(main):002:1* 1234 => { 'open' => '1234-1',
irb(main):003:2*               'analyzed' => '1234-2',
irb(main):004:2*               'feedback' => '1234-3',
irb(main):005:2*               'closed' => '1234-4'
irb(main):006:2>             },
irb(main):007:1* 3455 => { 'open' => '3455-1',
irb(main):008:2*               'analyzed' => '34552',
irb(main):009:2*               'feedback' => '3455-3',
irb(main):010:2*               'closed' => '3455-4'
irb(main):011:2>             }}
=> {3455=>{"open"=>"3455-1", "analyzed"=>"34552", "closed"=>"3455-4",
"feedback"=>"3455-3"}, 1234=>{"open"=>"1234-1", "analyzed"=>"1234-2",
"closed"=>"1234-4", "feedback"=>"1234-3"}}
irb(main):012:0> $DATA.each do |k,v|
irb(main):013:1* puts k if ( (v.has_key?('closed') ||
v.has_key?('feedback')) && ((v.has_key?('open') ||
v.has_key?('analyzed'))))
irb(main):014:1> end
3455
1234


Hope this helps,

Jesus.