On Dec 8, 2005, at 3:47 PM, Edward Rawde wrote:

> 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
>
>    @@code.push(@@byte)
>    @@address += 1
>
>   end
>
>   @@file.close
>
>  end
>
>  def Files.readCodeByte index
>   return @@code[index-@@offset]
>  end
>
>  def Files.setOffset anoffset
>   @@offset = anoffset
>  end
>
> end
>
>
>

def Files.read_file
   unless defined?(@@read_file_called)
   @@file = File.open("test.bin","rb")

   @@code = []

   @@address = 0

   while @@address < 65536 and (@@byte = @@file.getc) != nil

    @@code.push(@@byte)
    @@address += 1
    @@read_file_called = true
   end
   end

  def Files.readCodeByte index
    Files.read_file
     return @@code[index-@@offset]
  end

  def Files.setOffset anoffset
   Files.read_file
@@offset = anoffset
  end