# Why doesn't this file read work for me?

**URL:** <https://www.ruby-forum.com/t/why-doesnt-this-file-read-work-for-me/124926>\
**Category:** Ruby\
**Created:** [January 3, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/why-doesnt-this-file-read-work-for-me/124926 "2008-01-03T20:28:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 3, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/why-doesnt-this-file-read-work-for-me/124926/1 "2008-01-03T20:28:33Z")

</div>

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  
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\>

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 3, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/why-doesnt-this-file-read-work-for-me/124926/2 "2008-01-03T20:37:25Z")

</div>

On Jan 3, 2008 2:28 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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/)

> 

HTH,  
Michael G.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 3, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/why-doesnt-this-file-read-work-for-me/124926/3 "2008-01-03T20:38:36Z")

</div>

Alle giovedÃ¬ 3 gennaio 2008, Peter B. ha scritto:

> Peter  
> irb(main):006:1\> end  
> mathtype file.  
> =\> nil  
> irb(main):007:0\>

Because String#scan always returns an array, which is empty if there  
were no  
match, but which is always true. You should replace

if stuff.to\_s.scan(…) then

with

unless stuff.to\_s.scan(…).empty? then

If you only want to check whether stuff contains that substring, I think  
you  
should use String#match, not String#scan. String#match returns an object  
of  
class MatchData if there’s a match and nil otherwise, which allows you  
to use  
the conditional like you did your code (check the ri documentation for  
String#scan and String#match for more details).

By the way, why do you call to\_s on the result of File.open ? Isn’t it  
already  
a string?

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 3, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/why-doesnt-this-file-read-work-for-me/124926/4 "2008-01-03T20:54:42Z")

</div>

> 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/)
> 
> > 
> 
> HTH,  
> Michael G.

Thank you very much, Michael. I used .match and it works fine now.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 3, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/why-doesnt-this-file-read-work-for-me/124926/5 "2008-01-03T20:56:03Z")

</div>

> If you only want to check whether stuff contains that substring, I think  
> you  
> should use String#match, not String#scan. String#match returns an object  
> of  
> class MatchData if there’s a match and nil otherwise, which allows you  
> to use  
> the conditional like you did your code (check the ri documentation for  
> String#scan and String#match for more details).
> 
> By the way, why do you call to\_s on the result of File.open ? Isn’t it  
> already  
> a string?
> 
> I hope this helps
> 
> Stefano

Yup. This helps a lot. I used .match and it worked beautifully.  
Regarding the .to\_s thing, that’s just me being neurotic and thinking I  
needed it. No, I don’t need it.
