From: Michael Guterl Date: 2008-01-04T04:36:32+09:00 Subject: Re: Why doesn't this file read work for me? ------=_Part_25838_7944381.1199388993877 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Jan 3, 2008 2:28 PM, Peter Bailey wrote: > Hi, > I need to open files, just the first 75 bytes of them, and determine if > there's a string in the data. If the string is there, then, I do > something. If not, then I do something else. > > Here's my IRB try. I don't understand why it's coming back to me with a > positive, meaning, it seems to see the string, when, the string > definitely isn't in the file. > > Thanks, > Peter > > > L:\eps\fedreg>irb > irb(main):001:0> stuff = File.open("f3153013.eps") { |f| f.read(75) } > => "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 552 > 704\n%%HiResBoundingBox: 0.00" > irb(main):002:0> if stuff.to_s.scan(/^\%\%Creator: MathType/) then String#scan returns an array. So you can check the size. if stuff.to_s.scan(/^\%\%Creator: MathType/).size == 0 Or you can simple use String#match instead which is the behavior you are looking for. if stuff.to_s.match(/^\%\%Creator: MathType/) > > irb(main):003:1* puts "MathType file." > irb(main):004:1> else > irb(main):005:1* puts "NOT MathType file." > irb(main):006:1> end > mathtype file. > => nil > irb(main):007:0> > -- > Posted via http://www.ruby-forum.com/. > > HTH, Michael Guterl ------=_Part_25838_7944381.1199388993877--