The class below seems to do exactly what I want it to do provided
Files.read_file is called first.
But it would be nice not to have to call Files.read_file externally at
all
and have the other two methods automatically call it once, the first
time
either of them is used.
So far I’ve not been able to do this without getting “uninitialized
class
variable” errors. Is there a simple way to do it?
I’ve only been learning Ruby for two days so it’s quite possible I’ve
missed
the obvious.
Ed
class Files
def Files.read_file
@@file = File.open(“test.bin”,“rb”)
@@code = []
@@address = 0
while @@address < 65536 and (@@byte = @@file.getc) != nil
So far I’ve not been able to do this without getting “uninitialized class
variable” errors. Is there a simple way to do it?
I’ve only been learning Ruby for two days so it’s quite possible I’ve missed
the obvious.
def Files.read_file
File.open(FNAME, ‘rb’) do | @@file |
while @@address < 65536 and (@@byte = @@file.getc)
@@code.push(@@byte)
@@address += 1
end
end
end
def Files.readCodeByte(index)
@@file or read_file
@@code[index-@@offset]
end
def Files.setOffset(anoffset)
@@file or read_file
@@offset = anoffset
end
end