Module: Readlines::Count
- Included in:
- ReadDuc
- Defined in:
- lib/readlines/readlines/count.rb
Instance Method Summary collapse
-
#character_count_now(line_specific: false) ⇒ Object
Count the number of characters in the file or a specific line.
- #line_count_now ⇒ Object
-
#read_lines_now ⇒ Object
Read the entire content of the file.
-
#word_count_now(line_specific: false) ⇒ Object
Count the number of words in the file or a specific line.
Instance Method Details
#character_count_now(line_specific: false) ⇒ Object
Count the number of characters in the file or a specific line
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/readlines/readlines/count.rb', line 33 def character_count_now(line_specific: false) raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.read(@file_path) if line_specific.is_a?(Integer) line = content.lines[line_specific - 1] line.length else content.length end end |
#line_count_now ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/readlines/readlines/count.rb', line 8 def line_count_now raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) count = 0 content = ::File.read(@file_path) if @split_delimiter sections = content.split(@split_delimiter) sections.each do |section| count += 1 if @count_keyword && section.include?(@count_keyword) end else content.each_line do |line| count += 1 if @count_keyword.nil? || line.include?(@count_keyword) end end count end |
#read_lines_now ⇒ Object
Read the entire content of the file
26 27 28 29 30 |
# File 'lib/readlines/readlines/count.rb', line 26 def read_lines_now raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.read(@file_path) content end |
#word_count_now(line_specific: false) ⇒ Object
Count the number of words in the file or a specific line
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/readlines/readlines/count.rb', line 46 def word_count_now(line_specific: false) raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.read(@file_path) if line_specific.is_a?(Integer) line = content.lines[line_specific - 1] line.split.length else content.split.length end end |