Class: RubyGrep::File
- Inherits:
-
Object
- Object
- RubyGrep::File
- Defined in:
- lib/ruby-grep/file.rb
Instance Attribute Summary collapse
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
Instance Method Summary collapse
- #colorized_filename ⇒ Object
-
#initialize(fn) ⇒ File
constructor
A new instance of File.
-
#match(expression) ⇒ Object
this method selects lines from the file that match the expression, which may be a string or regexp.
- #max_line_number_length ⇒ Object
Constructor Details
Instance Attribute Details
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
7 8 9 |
# File 'lib/ruby-grep/file.rb', line 7 def file_name @file_name end |
Instance Method Details
#colorized_filename ⇒ Object
21 22 23 24 25 |
# File 'lib/ruby-grep/file.rb', line 21 def colorized_filename filename = @file_name.scan(/\/(\.?\w+\.\w+)|(\.\w+)/).join colorised_filename = filename.colorize(:yellow) @file_name.gsub(filename, colorised_filename) end |
#match(expression) ⇒ Object
this method selects lines from the file that match the expression, which may be a string or regexp.
17 18 19 |
# File 'lib/ruby-grep/file.rb', line 17 def match(expression) @lines.select { |line| line.contents.match(expression.value) } end |
#max_line_number_length ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ruby-grep/file.rb', line 27 def max_line_number_length return @max_line_number_length if @max_line_number_length @max_line_number_length = 1 @lines.each do |line| if line.number.to_s.length > @max_line_number_length @max_line_number_length = line.number.to_s.length end end @max_line_number_length end |