[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why doesn't this file read work for me?

Peter Bailey

1/3/2008 7:29:00 PM

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>
--
Posted via http://www.ruby-....

4 Answers

Michael Guterl

1/3/2008 7:37:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Jan 3, 2008 2:28 PM, Peter Bailey <pbailey@bna.com> 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-....
>
>
HTH,
Michael Guterl

Stefano Crocco

1/3/2008 7:37:00 PM

0

Alle gioved=C3=AC 3 gennaio 2008, Peter Bailey ha scritto:
> 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 =3D File.open("f3153013.eps") { |f| f.read(75) }
> =3D> "%!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.
> =3D> nil
> irb(main):007:0>

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

if stuff.to_s.scan(...) then

with=20

unless stuff.to_s.scan(...).empty? then

If you only want to check whether stuff contains that substring, I think yo=
u=20
should use String#match, not String#scan. String#match returns an object of=
=20
class MatchData if there's a match and nil otherwise, which allows you to u=
se=20
the conditional like you did your code (check the ri documentation for=20
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 alre=
ady=20
a string?

I hope this helps

Stefano

Peter Bailey

1/3/2008 7:55:00 PM

0


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

Thank you very much, Michael. I used .match and it works fine now.
--
Posted via http://www.ruby-....

Peter Bailey

1/3/2008 7:56:00 PM

0

> 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.
--
Posted via http://www.ruby-....