A mostly grep-compatible search tool for Ruby codebases.
By default, rbgrep takes the same flags as grep and prints the same
kind of output: a filename heading, then NN: hit lines. Add
--context and each hit in a .rb file is shown together with the
method definition and class that enclose it, located with the
Prism parser.
gem install rbgrep
rbgrep [-rn -w -i -E -F -A/-B/-C --include=GLOB] PATTERN [PATH...]
Plain search works like grep (including \| alternation in basic
regexps, or -E for extended ones):
$ rbgrep -n 'def visible?' app/models/news.rb
app/models/news.rb
55: def visible?(user=User.current)
With --context, the hit arrives with the code that gives it meaning —
the enclosing method body and the class frame, with -- marking elided
stretches:
$ rbgrep -n --context 'def visible?' app/models/news.rb
app/models/news.rb
20- class News < ApplicationRecord
--
55: def visible?(user=User.current)
56- !user.nil? && user.allowed_to?(:view_news, project)
57- end
--
105- end
A hit on a method definition shows the body (capped); a hit that merely
references something shows a few surrounding lines. Files under test/
and spec/ directories, and non-Ruby files, print plain hits.
--head N caps output by hits, returns each hit whole, and reports how
many were cut — unlike | head, you always know whether there was more:
$ rbgrep -rn --head 3 'def initialize' lib
...
... 48 more hits (raise --head to see them)
When reading from a pipe, rbgrep behaves exactly like grep — no
headings, no context — so ... | rbgrep -v foo works as expected.
rbgrep is designed to be handed to an AI coding agent as its search
tool. instruction.md is a ready-made instruction sheet:
tell your agent to read and follow it.
MIT. See LICENSE.txt.