From: anansi Date: 2007-06-07T03:00:11+09:00 Subject: best DRY solution for this task Hi, I have a certain problem here and can't find a proper DRY solution for this so I wondered if someone here has an idea and could help me. I'm reading a file byte for byte. This file is structured and I try to give out each structures element and the meaning of it. So let's pretend the file content would be this in bytes (you get it e.g. with file.read(7)): 1 3d 0 233 0 1 2f now I wanna read the file byte for byte and output if the first 3 bytes are equal 1 3d 0: startflag : good (1 3d 0) or if the check fails startflag : bad (2 3d 0) now the fourth byte is read and if it is 1 debg_mode : on if it is 0 : out and if it is 233 : don't know ... My problem with a DRY solution is that some bytes in this file are grouped together and have just together a meaning as the first three starting bytes: 1 3d 0 at the beginning and some bytes have alone a meaning as the fourth byte 233 in the example. And that everything has another ouput. So the first three bytes meaning is another than the one of the fourt or the fifth byte. My solution until now: target = open('testfile.txt','r') startflag = target.read(3) print("startflag: ") if startflag == $startflag # here I took a global var because "\x13d0" didn't work print(" good") else print(" bad") end startflag.each_byte do |x| printf("%x ",x) end puts "" dbg_mode = target.read(1) printf("dbg_mode: ") if dbg_mode == "\x1" print(" on") elseif dbg_mode == "\x255" print("don't know") else print(" bad") end dbg_mode.each_byte {|x| printf("%x ",x) } puts "" and so on and on and on... so how can I do this check and outputting better? Because there are many bytes with many different meanings and as said sometimes I need to read in 4 bytes then just one then again 2 bytes... is this at all possible to do dry-like? thanks -- greets one must still have chaos in oneself to be able to give birth to a dancing star