[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Not initialized

Edward Rawde

12/8/2005 8:43:00 PM

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


6 Answers

Logan Capaldo

12/8/2005 10:23:00 PM

0


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


Edward Rawde

12/8/2005 10:58:00 PM

0

"Logan Capaldo" <logancapaldo@gmail.com> wrote in message
news:83E0BDB7-5A89-4217-8440-EBF110F7B2FD@gmail.com...
>
> 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.
[snip]
>>
>
> def Files.read_file
> unless defined?(@@read_file_called)

That's what I needed. Thanks Logan.

> @@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
>
>


daz

12/8/2005 11:12:00 PM

0


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


daz



Edward Rawde

12/9/2005 12:05:00 PM

0

"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
>
>
>


daz

12/9/2005 7:39:00 PM

0


Edward Rawde wrote:
>
> 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?
>

Using String % (sprintf)
http://www.rubycentral.com/book/ref_c_string.html#...

I've added the ascii character (or '.' if it's not printable)

address = 0x3ff8
while address < 0x4000
byte = Files.readCodeByte(address)
puts '%04X %08b %02X %c' %
[address, byte, byte, ((32..126) === byte) ? byte : ?.]
address += 1
end

#-> 3FF8 01011011 5B [
#-> 3FF9 00111101 3D =
#-> 3FFA 01000000 40 @
#-> 3FFB 00001000 08 .
#-> 3FFC 01001011 4B K
#-> 3FFD 01010100 54 T
#-> 3FFE 10111000 B8 .
#-> 3FFF 00010010 12 .


daz



Edward Rawde

12/9/2005 9:22:00 PM

0

"daz" <dooby@d10.karoo.co.uk> wrote in message
news:wTWdnc22duLNQATeSa8jmw@karoo.co.uk...
>
[snip]
>
> Using String % (sprintf)
> http://www.rubycentral.com/book/ref_c_string.html#...
>
> I've added the ascii character (or '.' if it's not printable)
>
> address = 0x3ff8
> while address < 0x4000
> byte = Files.readCodeByte(address)
> puts '%04X %08b %02X %c' %
> [address, byte, byte, ((32..126) === byte) ? byte : ?.]
> address += 1
> end

I knew there would be a simple way.
I think it's time I bought that book.
Thanks for your help daz.

>
> #-> 3FF8 01011011 5B [
> #-> 3FF9 00111101 3D =
> #-> 3FFA 01000000 40 @
> #-> 3FFB 00001000 08 .
> #-> 3FFC 01001011 4B K
> #-> 3FFD 01010100 54 T
> #-> 3FFE 10111000 B8 .
> #-> 3FFF 00010010 12 .
>
>
> daz
>
>
>