Class: Raykit::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/text.rb

Class Method Summary collapse

Class Method Details

.copy_if_different(source, destination) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/raykit/text.rb', line 19

def self.copy_if_different(source, destination)
  if !File.exist?(destination)
    FileUtils.cp source, destination
  else
    source_text = IO.read(source)
    destination_text = IO.read(destination)
    if source_text != destination_text
      FileUtils.rm destination
      FileUtils.cp source, destination
    end
  end
end

.replace_in_file(filename, search, replace) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/raykit/text.rb', line 9

def self.replace_in_file(filename, search, replace)
  text1 = IO.read(filename)
  text2 = text1.gsub(search) { |_str| str = replace }
  unless text1 == text2
    File.open(filename, "w") { |f| f.puts text2 }
    return true
  end
  false
end

.replace_in_glob(glob, search, replace) ⇒ Object



5
6
7
# File 'lib/raykit/text.rb', line 5

def self.replace_in_glob(glob, search, replace)
  Dir.glob(glob).each { |f| replace_in_file(f, search, replace) }
end