public
Description: Simple grep library done the Ruby way by extending Dir and File
Homepage:
Clone URL: git://github.com/JasonKing/grep.git
JasonKing (author)
Thu Mar 12 18:26:13 -0700 2009
commit  325d536990584fa2ad7cd9ca85ce33710de5324b
tree    a2851ad80bfc94b3c593f0be15ede646d3b220c4
parent  90f85ab2d53bfad3158fe811262b58d715dce8a5
grep /
name age message
file README.markdown Loading commit data...
file grep.gemspec
directory lib/
directory test/
README.markdown

grep

Yes, there are already a couple of grep gems around - but they didn't do things the way I wanted, and weren't easy to fork. So here's mine.

Installation

To perform a system wide installation:

gem source -a http://gems.github.com
gem install JasonKing-grep

Just require it, it just reopens Dir and File and adds the grep instance method to each of them. You pass it a regular expression and it returns an array of hashes, very simple:

:match => Contains the MatchData object (so you can add parens and capture parts)
:file => Contains the File#path where the pattern matched (mainly useful for the Dir)
:lineno => Contains the line number where the pattern matched
:line => Contains the full line from the file where the pattern matched

That's it, I think that's the best grep you can have. Call it on a Dir object and it will drill down through the directory, matching as it goes. Call it on a File object and it will just match in that file.

Usage

require 'grep'
f = File.open('foo')
m = f.grep(/^\s*Include\s+(\S+)/)
m.first[:match][1]   # => captured parens expression

Contributors

  • Jason King (JasonKing)