From: Ben Nagy Date: 2006-10-23T22:03:47+09:00 Subject: Re: Binary file modification > -----Original Message----- > From: list-bounce@example.com > [mailto:list-bounce@example.com] On Behalf Of Rob Lee > Sent: Monday, October 23, 2006 6:06 PM > To: ruby-talk ML > Subject: Re: Binary file modification > > Ben Nagy wrote: [...] > > num=1024 > > num.to_s(16)[0..2].instance_eval {(self.reverse + '0' * (6 - > > self.length)).reverse}.scan(/../).inject('') {|s,byte| s << > > byte.to_i(16)} > > > > I'm intrigued by this, I can see you are repacking the int value into > three bytes but I'm still unclear as to how you would then > add this to > the file. It's already packed. The return value of the long expression will be a 3 byte string which you can insert as you like (print it to the IO handle, << it to another string etc etc). The accumulator in the inject method is a string {|str,byte| ... might have been more readable, sorry. ben