I have a parser that reads files. Inside a file, you can declare a filename and the parser will go and read that one, then when it is done, pick up right where it left off and continue. This can happen as many levels deep as you want.
Sounds pretty easy so far. All I want to do is print out the file names and line numbers.
I have a class called FileReader that looks like this:
class FileReader
attr_accessor :filename, :lineNumber
def initialize(filename)
@filename = filename
@lineNumber = 0
end
def readFile()
# pseudocode to make this easy
open @filename
while (lines)
@lineNumber = @lineNumber + 1
if(line startsWith ('File:'))
FileReader.new(line).readFile()
end
puts 'read ' + @filename + ' at ' + @lineNumber.to_s()
end
puts 'EOF'
end
end
Simple enough. So lets say I have a file that refers other files like this. File1->File2->File3. This is what it looks like:
read File1 at 1
read File1 at 2
read File1 at 3
read File2 at 1
read File2 at 2
read File2 at 3
read File2 at 4
read File3 at 1
read File3 at 2
read File3 at 3
read File3 at 4
read File3 at 5
EOF
read File3 at 5
read File3 at 6
read File3 at 7
read File3 at 8
EOF
read File2 at 4
read File2 at 5
read File2 at 6
read File2 at 7
read File2 at 8
read File2 at 9
read File2 at 10
read File2 at 11
And that doesnt make any sense to me.
File 1 has 11 lines
File 2 has 8 lines
File 3 has 4 lines
I would assume creating a new object would have its own scope that doesn't affect a parent object.