Module: Readlines::Replace
- Included in:
- ReadDuc
- Defined in:
- lib/readlines/readlines/replace.rb
Instance Method Summary collapse
- #replace_csv_value_now(column_index, old_value, new_value) ⇒ Object
- #replace_multiple_patterns_now(pattern_replacement_hash) ⇒ Object
- #replace_now(pattern, replacement) ⇒ Object
- #replace_special_characters_now(replacement) ⇒ Object
Instance Method Details
#replace_csv_value_now(column_index, old_value, new_value) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/readlines/readlines/replace.rb', line 37 def replace_csv_value_now(column_index, old_value, new_value) 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(',') values[column_index] = values[column_index] == old_value ? new_value : values[column_index] values.join(',') end.join("\n") ::File.write(@file_path, updated_content) updated_content end |
#replace_multiple_patterns_now(pattern_replacement_hash) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/readlines/readlines/replace.rb', line 26 def replace_multiple_patterns_now(pattern_replacement_hash) raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.read(@file_path) pattern_replacement_hash.each do |pattern, replacement| content.gsub!(pattern, replacement) end ::File.write(@file_path, content) content end |
#replace_now(pattern, replacement) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/readlines/readlines/replace.rb', line 8 def replace_now(pattern, replacement) raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.read(@file_path) updated_content = content.gsub(pattern, replacement) ::File.write(@file_path, updated_content) updated_content end |
#replace_special_characters_now(replacement) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/readlines/readlines/replace.rb', line 17 def replace_special_characters_now(replacement) raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path) content = ::File.read(@file_path, encoding: 'UTF-8') updated_content = content.gsub(/[^[:alnum:]\s]/, replacement) ::File.write(@file_path, updated_content) updated_content end |