# Grepper: object-oriented grepping

**URL:** <https://www.ruby-forum.com/t/grepper-object-oriented-grepping/156294>\
**Category:** Ruby\
**Created:** [December 24, 2008, 2:54am UTC](https://www.ruby-forum.com/t/grepper-object-oriented-grepping/156294 "2008-12-24T02:54:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_ASBlack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@David\_ASBlack](https://www.ruby-forum.com/u/David_ASBlack)\
**Post date:** [December 24, 2008, 2:54am UTC](https://www.ruby-forum.com/t/grepper-object-oriented-grepping/156294/1 "2008-12-24T02:54:19Z")

</div>

Hi –

If you feel the need to do simple object-oriented grepping, have a  
look at the Grepper class, which is in a project I’ve started on  
RubyForge ([http://rubyforge.org/projects/grepper/](http://rubyforge.org/projects/grepper/)). You give a Grepper  
object a list of files, a pattern, some options… and it creates a  
result set, which you can then walk through.

g = Grepper.new  
g.files = %w{ one.txt two.txt three.txt }  
g.options = %w{ B2 } # two lines of before-context  
g.run

g.results.each do |file, result|  
result.matches.each do |lineno,before,line,after|

etc.

There’s also a Formatter, which does its best to emulate standard  
grep(1) output. In fact the whole thing started as an experiment in  
implementing grep in Ruby. I then decided to create a layer that just  
did the grep logic, on top of which I could build the output  
formatting and so forth.

Let me know if any bugs, questions, etc.

David

---

<div class="post-metadata">

**Author:** ![David\_ASBlack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@David\_ASBlack](https://www.ruby-forum.com/u/David_ASBlack)\
**Post date:** [December 24, 2008, 3:26am UTC](https://www.ruby-forum.com/t/grepper-object-oriented-grepping/156294/2 "2008-12-24T03:26:28Z")

</div>

David A. Black escreveu:

> g.options = %w{ B2 } # two lines of before-context  
> did the grep logic, on top of which I could build the output  
> formatting and so forth.
> 
> Let me know if any bugs, questions, etc.
> 
> David

Hi David,  
Very cool your idea! I will take a look and make some codes to see the  
power of the Grepper.  
Hugs,

- Tiago
