Class: RubySearch::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_search/search.rb

Instance Method Summary collapse

Instance Method Details

#grep(regex) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby_search/search.rb', line 5

def grep(regex)
  $LOADED_FEATURES
    .select {|f| f =~ /\.rb\z/ }
    .lazy
    .select {|f| File.exist?(f) }
    .map {|f| [f, File.read(f)] }
    .reject {|x| x[1].to_s.empty? }
    .map do |file, stream|
      matches = stream
        .each_line
        .map { |line| regex.match(line) }
        .reject(&:nil?)
        .each.with_index(1)
        .to_a
      [file, matches]
    end
end