Class: RGrep
- Inherits:
-
Object
- Object
- RGrep
- Defined in:
- lib/rgrep.rb
Overview
RGrep class
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(regexp, *uris) ⇒ RGrep
constructor
A new instance of RGrep.
- #rgrep_with(regexp, filename) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(regexp, *uris) ⇒ RGrep
Returns a new instance of RGrep.
7 8 9 10 11 12 |
# File 'lib/rgrep.rb', line 7 def initialize regexp, *uris filenames = uris.select {|uri| File.file? uri} @result = filenames.map do |filename| rgrep_with regexp, filename end.inject([]) {|ret,val| ret += val} end |
Class Method Details
.cli(*argv) ⇒ Object
3 4 5 |
# File 'lib/rgrep.rb', line 3 def self.cli *argv puts new(*argv).to_s end |
Instance Method Details
#rgrep_with(regexp, filename) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/rgrep.rb', line 14 def rgrep_with regexp, filename result = [] File.readlines(filename).each_with_index do |contents,index| result.push [filename, index, contents] if contents =~ Regexp.new(regexp) end return result end |
#to_s ⇒ Object
22 23 24 25 26 |
# File 'lib/rgrep.rb', line 22 def to_s @result.map do |filename,index,contents| "#{filename}:#{index}:#{contents}".chomp end.join("\n") end |