On Thu, Jan 7, 2010 at 10:55 AM, Jagadeesh <mnjagadeesh / gmail.com> wrote:
> On Jan 7, 2:33=A0pm, Jes=FAs Gabriel y Gal=E1n <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 =3D> { 'open' =3D> '1234-1',
>> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '1234-2',
>> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '1234-3',
>> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '1234-4'
>> > =A0 =A0 =A0 =A0 =A0 =A0 }
>> > 3455 =3D> { 'open' =3D> '3455-1',
>> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '34552',
>> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '3455-3',
>> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '3455-4'
>> > =A0 =A0 =A0 =A0 =A0 =A0 }
>>
>> > My task is to display numbers [like 1234 or 3455] if they have ('open'
>> > OR 'analyzed') AND ('closed' OR 'feedback') =A0keys. I tried following
>> > code
>>
>> > puts numbers if $DATA[pr].has_key?( ( ('closed' || 'feedback')
>> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 && ('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') )
>> =3D> "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=3D>{'1234' =3D> { 'open' =3D> '1234-1',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '1234-2',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '1234-3',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '1234-4'
> =A0 =A0 =A0 =A0 =A0 =A0 },
> =A0 =A0 =A0 =A0'3455' =3D> { 'open' =3D> '3455-1',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 'analyzed' =3D> '34552',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 'feedback' =3D> '3455-3',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 'closed' =3D> '3455-4'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> }
>
> and am reading it in loop so pr will have 1234, 3455 etc.

As Stefano said:

irb(main):001:0> $DATA =3D {
irb(main):002:1* 1234 =3D> { 'open' =3D> '1234-1',
irb(main):003:2*               'analyzed' =3D> '1234-2',
irb(main):004:2*               'feedback' =3D> '1234-3',
irb(main):005:2*               'closed' =3D> '1234-4'
irb(main):006:2>             },
irb(main):007:1* 3455 =3D> { 'open' =3D> '3455-1',
irb(main):008:2*               'analyzed' =3D> '34552',
irb(main):009:2*               'feedback' =3D> '3455-3',
irb(main):010:2*               'closed' =3D> '3455-4'
irb(main):011:2>             }}
=3D> {3455=3D>{"open"=3D>"3455-1", "analyzed"=3D>"34552", "closed"=3D>"3455=
-4",
"feedback"=3D>"3455-3"}, 1234=3D>{"open"=3D>"1234-1", "analyzed"=3D>"1234-2=
",
"closed"=3D>"1234-4", "feedback"=3D>"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.