Module: LineContaining

Defined in:
lib/line_containing.rb,
lib/line_containing/version.rb

Constant Summary collapse

VERSION =
"0.0.0"

Class Method Summary collapse

Class Method Details

.add_after(str_orig, str_add, path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/line_containing.rb', line 22

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|
    if line.include? str_orig
      file_w.write(line)
      file_w.write("\n") if line[-1] != "\n"
      file_w.write("#{str_add}\n")
    else
      file_w.write(line)
    end
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end

.add_before(str_orig, str_add, path) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/line_containing.rb', line 4

def self.add_before(str_orig, str_add, path)
  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
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end

.delete(str_orig, path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/line_containing.rb', line 56

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|
    if line.include? str_orig
      # Print NOTHING
    else
      file_w.write(line)
    end
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end

.replace(str_orig, str_new, path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/line_containing.rb', line 40

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|
    if line.include? str_orig
      file_w.write("#{str_new}\n")
    else
      file_w.write(line)
    end
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end