[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[ANN] Grepper: object-oriented grepping

David A. Black

12/24/2008 1:45:00 AM

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

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

1 Answer

Tiago Nogueira

12/24/2008 2:17:00 AM

0

David A. Black escreveu:
> 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/project...). 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
>
Hi David,
Very cool your idea! I will take a look and make some codes to see the
power of the Grepper.
Hugs,
- Tiago