lib/line_containing.rb in line_containing-0.0.0 vs lib/line_containing.rb in line_containing-0.1.0
- old
+ new
@@ -1,26 +1,23 @@
-require "line_containing/version"
+require 'line_containing/version'
+#
module LineContaining
def self.add_before(str_orig, str_add, path)
- system("pwd")
+ system('pwd')
path_old = path
path_new = "#{path_old}.new"
file_w = open(path_new, 'w')
File.readlines(path_old).each do |line|
- if line.include? str_orig
- file_w.write("#{str_add}\n")
- file_w.write(line)
- else
- file_w.write(line)
- end
+ file_w.write("#{str_add}\n") if line.include? str_orig
+ file_w.write(line)
end
file_w.close
system("rm #{path_old}")
system("mv #{path_new} #{path_old}")
end
-
+
def self.add_after(str_orig, str_add, path)
path_old = path
path_new = "#{path_old}.new"
file_w = open(path_new, 'w')
File.readlines(path_old).each do |line|
@@ -34,11 +31,11 @@
end
file_w.close
system("rm #{path_old}")
system("mv #{path_new} #{path_old}")
end
-
+
def self.replace(str_orig, str_new, path)
path_old = path
path_new = "#{path_old}.new"
file_w = open(path_new, 'w')
File.readlines(path_old).each do |line|
@@ -50,11 +47,11 @@
end
file_w.close
system("rm #{path_old}")
system("mv #{path_new} #{path_old}")
end
-
+
def self.delete(str_orig, path)
path_old = path
path_new = "#{path_old}.new"
file_w = open(path_new, 'w')
File.readlines(path_old).each do |line|
@@ -65,7 +62,34 @@
end
end
file_w.close
system("rm #{path_old}")
system("mv #{path_new} #{path_old}")
+ end
+
+ def self.delete_between(str1, str2, path)
+ path_old = path
+ path_new = "#{path_old}.new"
+ file_w = open(path_new, 'w')
+ to_delete = false
+ File.readlines(path_old).each do |line|
+ if line.include? str1
+ file_w.write(line)
+ to_delete = true
+ elsif line.include? str2
+ file_w.write(line)
+ to_delete = false
+ elsif to_delete == false
+ file_w.write(line)
+ end
+ end
+ file_w.close
+ system("rm #{path_old}")
+ system("mv #{path_new} #{path_old}")
+ end
+
+ def self.delete_between_plus(str1, str2, path)
+ delete_between(str1, str2, path)
+ delete(str1, path)
+ delete(str2, path)
end
end