Class Instanciation in loop

Hi everyone !

I am a beginner in Ruby and i am faced to a problem I do not understand.

I have two classes: Subset, Parser
Parser class parses an array (tab) which contains worlds from a file and
creates
Subset objects and adds them in two others arrays (@@good, @@bad).

This file looks like:
Good
{
Subgp1
{
file1
}
}
Bad
{
Subgp2
{
file2
}
}

For example :

j = tab.find_index(“Bad”)
i = 1
while i < tab.size do
c = Subset.new(tab[i])
i = i + 1

This method parse is a subparser which parse the containning of SubgpX

elements (here, file1 and file2) and put them in an Subset class

array.
i = c.parse(tab, i)
if tab[i] < j then
@good << c
else
@@bad << c
end
i = i + 1
end

When I do a prettyprint of @@good and @@bad, @@good have the same
elements as
in @@bad, and others elements which should be in @@good disappeared.

Here is the output I have:

Good Tests :
Subgroup name: Subgp2
File: file2
Bad Tests :
Subgroup name: Subgp2
File: file2

Here is the output I want:

Good Tests :
Subgroup name: Subgp1
File: file1
Bad Tests :
Subgroup name: Subgp2
File: file2

Can you explain me why I have this behaviour in my script ?

Thanks by advance !

Nobody wants to help me ?

On Thursday 03 February 2011 21:55:39 Alize P. wrote:

Good
file2

This method parse is a subparser which parse the containning of SubgpX

When I do a prettyprint of @@good and @@bad, @@good have the same
elements as
in @@bad, and others elements which should be in @@good disappeared.

I think there’s a typo in your code:
@good << c
should be

@@good << c

Stefano

On Fri, Feb 4, 2011 at 3:30 PM, Alize P. [email protected]
wrote:

Nobody wants to help me ?

Hi Alizee,

maybe because you did not provide all the relevant data/codes/classes/
concerning the particular problem. it is time consuming if we just
guess…

anyway,

the ff is a sample test on my irb console. just thought you might
compare it w your scheme,

File.open(“filetest.txt”,“r”){|f| puts f.read}

Good
{
Subgp1
{
file1
}
}
Bad
{
Subgp2
{
file2
}
}
#=> nil

File.open(“filetest.txt”,“r”){|f| s=f.read}
#=> “Good\n{\n Subgp1\n {\n file1\n }\n}\nBad\n{\n Subgp2\n {\n
file2\n }\n}”

s
#=> “Good\n{\n Subgp1\n {\n file1\n }\n}\nBad\n{\n Subgp2\n {\n
file2\n }\n}”

File.open(“filetest2.txt”,“w”) do |f|
s.scan(/(\w+)\n{\s*(\w+)\s*{\s*(\w+)\s*}\s*}/mx).each do
|group,subgroup,file|
f.write “Group:#{group}\n\tSubgroup:#{subgroup}\n\t\tFile:#{file}\n”
end
end
#=> [[“Good”, “Subgp1”, “file1”], [“Bad”, “Subgp2”, “file2”]]

File.open(“filetest2.txt”,“r”) do |f|
puts f.read
end

Group:Good
Subgroup:Subgp1
File:file1
Group:Bad
Subgroup:Subgp2
File:file2
#=> nil

best regards -botp