1

I'm looking for an elegant way to scan a ruby File containing binary data for a regular expression.

To be clear, my hangup is not confusion about applying regexes to binary. It's just that because the file is binary, I'd like to avoid gets and readline. And I'd like to avoid reading the entire file at once.

Is there a function in the ruby core that will do this? Or do I need to build and manage my own buffer?

0

1 Answer 1

2

Ruby doesn't really care if strings are UTF-8, Latin-1, or binary. They're just strings. Regular expressions work just fine. The encoding property should be set accordingly, obviously, and the file opened in the correct mode, but other than that, not a big deal.

When working with binary files, lines are irrelevant, so use tools like IO#read to pull in arbitrary chunks.

If the file isn't especially large, read in the whole file at once and work with it that way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.