From: Bertram Scharpf Date: 2007-08-21T18:06:26+09:00 Subject: Re: Test if file is binary ? Hi, Am Dienstag, 21. Aug 2007, 15:57:13 +0900 schrieb Rebhan, Gilbert: > From: dima [mailto:dejan.dimic@gmail.com] > Sent: Tuesday, August 21, 2007 8:50 AM > Subject: Re: Test if file is binary ? > > On Aug 21, 8:04 am, "Rebhan, Gilbert" > wrote: > >> how to test if a file is binary or not ? > >> > >> There ain't something like File.binary = > >> NoMethodError: undefined method `binary?' for File:Class > > >What to you need to achieve with this is_binary? method? > > For example this information is needed to decide whether > cvs should handle that file / that fileextension as binary or ascii I'm impressed by the solutions of Alex and Robert. Anyway I suppose in most cases a test on one single null character will suffice. Something like this: class File def binary? while (b=f.read(256)) do return true if b[ "\0"] end end end Yet I recommend first to review whether you want to read the file later. In this case you may abort reading when the file fails a more sophisticated filetype check. Dividing files into "text" and "binary" is the archetype misdesign in the operating system you use. (Is there anything designed well (besides Outlook, of course?)) The distinction doesn't refer to the files _contents_ but how to the file is _treated_ when it is being read or written. In "rb"/"wb" modes files are left how they are, in "r"/"w" modes Windows programmers get line ends "\r\n" translated into "\n" what disturbs file positions and string lengths. I think the only purpose of this is to detain programmers from doing anything a non-Microsoft way. Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de