Newbie question

Hello all and happy new year. I hope this is the right mailing list for
this question. If not please redirect me and accept my apologies. Thanks
in
advance.
I just started learning Ruby. I have good experience in Java and Perl
and
have read most of the doc on Ruby that I can get my hands on. I started
on a
project to build the DICOM netwrok library as a way to learn Ruby. I
have
done quite bit of coding on this project and have a working code. Well
when
I say quite a bit, it is about 5% done!! What I am looking for is
RubyIdioms
and if there are better ways of doing things as opposed to what I have
already done. Basically DICOM messages are binary and we need to read
the
data and manipulate byes. We will have to construct integers or floats
from
bytes or pair of bytes and the next few bytes could be a string etc. I
have
pasted some part of the code and want your opinion on this. Also, once
this
project has reached a point where it could be used, I will release it to
the
community.

What I am looking for is recomendation to improve the speed of the code
in
processing the bytes. is ‘slice’ the best way to do what I am doing. I
am
going to re-arrange the code, so dont worry about the neatness at this
point.

Thans and here is the code.

session comes from

server = TCPServer.new(‘localhost’, port)
p “waiting on connection”
while (session = server.accept) …

def ae_6 session
#AE_6
p “Inside ae_6”
#stop ARTIM and issue and a-associate indiction primitive.

all we have to do here is suck in the whole PDU, that amounts to

sending
the indication primitive.
pdu=session.getc # reserved, neglect this byte
pduLen=session.read(4).unpack(‘N’)[0] # unsigned integer 4 bytes long
print “PDU Length =”, pduLen,"\n"
pdu=session.read(pduLen) # this is the full PDU
#version=pdu.slice(0…1).unpack(‘n’)[0]
#p version
calledAETitle = pdu.slice(4…19) # string of 16 chars
callingAETitle = pdu.slice(20…35) # string of 16 chars
p calledAETitle
p callingAETitle
#pdu.each_byte {|c| print c,"|" }
#reserved=pdu.slice(36…67) # ignore this
#next item type should be 10H which is “Application Context” which is
always set to “1.2.840.10008.3.1.1.1”
itemType=pdu.slice(68…68).unpack(‘C’)[0]
#printf( “ItemType=%XH\n”, itemType)
#ignore the next byte, byte # 69
itemLength=pdu.slice(70…71).unpack(‘n’)[0]
contextName=pdu.slice(72…72+itemLength-1)
p contextName

Next we read presentation context items

start = 72+itemLength
finish = 0
while finish < pduLen
finish = getNextPresentationContext(start, pdu)
print “Finish = “,finish,” outside proc\n”
start=finish+1
end
end

def getNextPresentationContext(start, pdu)
finish = start
itemType=pdu.slice(start…finish).unpack(‘C’)[0] #should be20H
start = finish+2 # added 2 here since we need to skip on byte which is
reserved
finish = start + 1 # total of two bytes
itemLength=pdu.slice(start…finish).unpack(‘n’)[0]
#printf( “FIRST ItemType=%XH, itemLength=%d\n”, itemType,itemLength)
start = finish+1
finish = start
presContextId=pdu.slice(start…finish).unpack(‘C’)[0]

next 3 bytes needs to be ignored, they are reserved hence +4 below

start = finish +4
finish = start
itemType=pdu.slice(start…finish).unpack(‘C’)[0]
start = finish+2 # added 2 here since we need to skip on byte which is
reserved
finish = start + 1 # total of two bytes
itemLength=pdu.slice(start…finish).unpack(‘n’)[0]
#printf( “SECOND ItemType=%XH, itemLength=%d\n”, itemType,itemLength)
start = finish+1
finish = start + itemLength-1 # total of two bytes
abstractSyntax=pdu.slice(start…finish)

start = finish +1
finish = start
itemType=pdu.slice(start…finish).unpack(‘C’)[0]
while itemType == 64
start = finish+2 # added 2 here since we need to skip one byte which
is
reserved
finish = start + 1 # total of two bytes
itemLength=pdu.slice(start…finish).unpack(‘n’)[0]
#printf( “LATER ItemType=%XH, itemLength=%d\n”, itemType,itemLength)
start = finish+1
finish = start + itemLength-1 # total of two bytes
transferSyntax=pdu.slice(start…finish)
printf(“id=%d, AbstractSyntax =%s, TxSyntax=%s\n”,
presContextId,abstractSyntax,transferSyntax)
start=finish+1
finish = start
itemType=pdu.slice(start…finish).unpack(‘C’)[0]
end
return finish-1