From: Daniel Berger Date: 2003-07-16T01:38:24+09:00 Subject: Re: Determining if a String is marshalled Brian Candler wrote: > On Wed, Jul 16, 2003 at 12:45:46AM +0900, Daniel Berger wrote: > >>I was just wondering if there's a way to determine if a string has been >>marshalled without visual inspection. Something along the lines of >>"string.marshalled?" or "string.kind_of?(MarshaledString)". Is there a >>regex I could use? > > > Check for the first two bytes, which are the major/minor version number. > Rather than hard-code it, you could do > > marshall_sig = Marshal.dump("")[0..1] > marshall_check = /\A#{marshall_sig}/ > > irb(main):004:0> "abc" =~ marshall_check > => nil > irb(main):005:0> Marshal.dump(123) =~ marshall_check > => 0 > > Of course, the string might actually start with those bytes, but they are > low ("\004\006" on my machine) so unlikely to occur in a normal string. > > Regards, > > Brian. Thanks Brian and Matz for the info. My only concern might be that a pack'd string would inadvertantly pass this test. I guess that's what begin/rescue is for, though. :) Regards, Dan