"daz" <dooby / d10.karoo.co.uk> wrote in message 
news:tBqdnSwymdRWIAXeSa8jmw / karoo.co.uk...
>
> Edward Rawde wrote:
>> 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
>
> Hi Ed,
>
> (I saw your acknowledgement to Logan)
>
> Here's another:
> ..............
>
> FNAME = 'test.bin'
>
> class Files
>  @@file = nil
>  @@code = []
>  @@address = 0
>  @@offset  = 0
>
>  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
>

Thanks daz, it will be useful to figure out exactly how that works.

Now I have another problem.
This code outputs bytes in hex and binary from a 16K file:

Files.setOffset 0x0000

address = 0x0000

while address < 0x4000

 byte = Files.readCodeByte(address)

 puts address.to_s(16) + " " + byte.to_s(2).upcase + " " + byte.to_s(16)

 address += 1

end

But I want fixed width. So if byte 0 is at address 0 I want 0000 00000000 00

I can probably figure some code to add the leading zeros but what's the most 
efficient way to do that in Ruby?

Thanks

Ed

>
> daz
>
>
>