Class Instanciation in loop

  • Thread starter Alizée Penel
  • Start date
A

Alizée Penel

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 + 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 < 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 !
 
S

Stefano Crocco

Hi everyone !
=20
I am a beginner in Ruby and i am faced to a problem I do not understa= nd.
=20
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).
=20
This file looks like:
Good
{
Subgp1
{
file1
}
}
Bad
{
Subgp2
{
file2
}
}
=20
For example :
=20
j =3D tab.find_index("Bad")
i =3D 1
while i < tab.size do
c =3D Subset.new(tab)
i =3D i + 1
# This method parse is a subparser which parse the containning of Sub= gpX
# elements (here, file1 and file2) and put them in an Subset class
array.
i =3D c.parse(tab, i)
if tab < j then
@good << c
else
@@bad << c
end
i =3D i + 1
end
=20
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
 
B

botp

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
}
}
#=3D> nil

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

s
#=3D> "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
#=3D> [["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
#=3D> nil


best regards -botp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top