Module: Readlines::Delete
- Included in:
- ReadDuc
- Defined in:
- lib/readlines/readlines/delete.rb
Instance Method Summary collapse
-
#delete_csv_columns_now(column_indices) ⇒ Object
Delete specific columns from a CSV file.
-
#delete_duplicate_lines_now ⇒ Object
Delete duplicate lines from the file.
- #delete_empty_lines_now ⇒ Object
-
#delete_lines_now(line_numbers, delete_space: false) ⇒ Object
Delete specific lines from the file and optionally remove empty lines.
- #delete_right_left_separator_now(separator, delete_right: false, delete_left: false, keep_separator: false) ⇒ Object
-
#delete_unwanted_characters_now(characters) ⇒ Object
Delete unwanted characters from the file.
Instance Method Details
#delete_csv_columns_now(column_indices) ⇒ Object
Delete specific columns from a CSV file
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/readlines/readlines/delete.rb', line 53 def delete_csv_columns_now(column_indices) raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.readlines(@file_path) updated_content = content.map do |line| values = line.chomp.split(',') column_indices.sort.reverse.each { |index| values.delete_at(index) } values.join(',') end.join("\n") ::File.write(@file_path, updated_content) updated_content end |
#delete_duplicate_lines_now ⇒ Object
Delete duplicate lines from the file
43 44 45 46 47 48 49 50 |
# File 'lib/readlines/readlines/delete.rb', line 43 def delete_duplicate_lines_now raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.readlines(@file_path) unique_content = content.uniq.join ::File.write(@file_path, unique_content) unique_content end |
#delete_empty_lines_now ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/readlines/readlines/delete.rb', line 8 def delete_empty_lines_now raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.read(@file_path) updated_content = content.lines.reject { |line| line.strip.empty? }.join ::File.write(@file_path, updated_content) updated_content end |
#delete_lines_now(line_numbers, delete_space: false) ⇒ Object
Delete specific lines from the file and optionally remove empty lines
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/readlines/readlines/delete.rb', line 27 def delete_lines_now(line_numbers, delete_space: false) raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.readlines(@file_path) updated_content = content.reject.with_index { |_, index| line_numbers.include?(index + 1) }.join ::File.write(@file_path, updated_content) if delete_space remove_empty_lines end updated_content end |
#delete_right_left_separator_now(separator, delete_right: false, delete_left: false, keep_separator: false) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/readlines/readlines/delete.rb', line 66 def delete_right_left_separator_now(separator, delete_right: false, delete_left: false, keep_separator: false) raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) result = ::File.readlines(@file_path).map do |line| line.strip! next if line.empty? parts = line.split(separator, 2) separator_keep = keep_separator ? separator : '' if delete_right && parts.size > 1 parts.first + separator_keep elsif delete_left && parts.size > 1 separator_keep + parts.last else line end end.compact ::File.write(@file_path, result.join("\n")) result end |
#delete_unwanted_characters_now(characters) ⇒ Object
Delete unwanted characters from the file
17 18 19 20 21 22 23 24 |
# File 'lib/readlines/readlines/delete.rb', line 17 def delete_unwanted_characters_now(characters) raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.read(@file_path) updated_content = content.gsub(/[#{Regexp.escape(characters)}]/, '') ::File.write(@file_path, updated_content) updated_content end |