Module: StringInFile
- Defined in:
- lib/string_in_file.rb,
lib/string_in_file/version.rb
Constant Summary collapse
- VERSION =
'1.0.2'.freeze
Class Method Summary collapse
- .add_beginning(str_to_add, path) ⇒ Object
- .add_end(str_to_add, path) ⇒ Object
- .present(str_to_find, path) ⇒ Object
-
.read(path) ⇒ Object
Put contents of file into string Thanks to Keith R.
-
.replace(string1, string2, path) ⇒ Object
Replace all instances of string1 in a file with string2 If string1 does not exist in the file, or if the file does not exist, no action is taken.
-
.write(str_to_write, path) ⇒ Object
Writes the value of a string into a file.
Class Method Details
.add_beginning(str_to_add, path) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/string_in_file.rb', line 55 def self.add_beginning(str_to_add, path) path_old = "#{path}.old" system("mv #{path} #{path_old}") # Create a new file and write to it file_to_write = File.open(path, 'w') file_to_write.puts str_to_add File.foreach(path_old) do |line| file_to_write.puts line end file_to_write.close # Delete the old file system("rm -f #{path_old}") end |
.add_end(str_to_add, path) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/string_in_file.rb', line 71 def self.add_end(str_to_add, path) path_old = "#{path}.old" system("mv #{path} #{path_old}") # Create a new file and write to it file_to_write = File.open(path, 'w') File.foreach(path_old) do |line| file_to_write.puts line end file_to_write.puts str_to_add file_to_write.close # Delete the old file system("rm -f #{path_old}") end |
.present(str_to_find, path) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/string_in_file.rb', line 42 def self.present(str_to_find, path) begin # loop through each line in the file File.foreach(path) do |line| # does the line contain our string? return true if line.include?(str_to_find) end return false rescue return false end end |
.read(path) ⇒ Object
Put contents of file into string Thanks to Keith R. Bennett for suggesting this solution on ruby.mn. Please note that newlines are removed. Thus, this is designed to work with 1-line strings.
9 10 11 12 |
# File 'lib/string_in_file.rb', line 9 def self.read(path) str_output = File.readlines(path).join('') str_output = str_output.delete("\n") end |
.replace(string1, string2, path) ⇒ Object
Replace all instances of string1 in a file with string2 If string1 does not exist in the file, or if the file does not exist, no action is taken.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/string_in_file.rb', line 22 def self.replace(string1, string2, path) path_old = "#{path}.old" system("mv #{path} #{path_old}") # Create a new file and write to it file_to_write = File.open(path, 'w') File.foreach(path_old) do |line| # does the line contain our string? if line.include?(string1) file_to_write.puts line.gsub!(string1, string2) else file_to_write.puts line end end file_to_write.close # Delete the old file system("rm -f #{path_old}") end |
.write(str_to_write, path) ⇒ Object
Writes the value of a string into a file
15 16 17 |
# File 'lib/string_in_file.rb', line 15 def self.write(str_to_write, path) File.open(path, 'w') { |f| f.write(str_to_write) } end |