Your Ruby snippet is slower for two reasons:
- IO.foreach is lazy, but then you call `Array#grep` on it which allocate a huge Array
- `grep` populate the "last_match" object, `String#match?` is much faster.
Try rifraf's snippet:
https://news.ycombinator.com/item?id=24597067, it should be as fast as Python.