Class: Dorian::Replace

Inherits:
Object
  • Object
show all
Defined in:
lib/dorian/replace.rb

Class Method Summary collapse

Class Method Details

.run ⇒ Object



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

def self.run
  if ARGV.size < 2
    puts "USAGE: replace FROM TO FILES..."
    puts "USAGE: ... | replace FROM TO"
    exit
  end

  from = ARGV[0]
  to = ARGV[1]
  files = ARGV.size > 2 ? ARGV[2..-1] : STDIN.each_line.to_a.map(&:strip)

  files.each do |file|
    next if File.directory?(file)
    File.write(file, File.read(file).gsub(from, to))
  end
end