Class: SpaceRace

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

Constant Summary collapse

VERSION =
'0.0.3'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SpaceRace

Returns a new instance of SpaceRace.



6
7
8
9
10
11
12
13
# File 'lib/spacerace.rb', line 6

def initialize(options = {})
  @space       = options[:space]       || '\t'
  @replacement = options[:replacement] || "  "
  @reduce      = options[:reduce]      || 1
  @no_backup   = options[:nobackup]    || false
  
  # puts "Space: `#{@space}`", "Replacement: `#{@replacement}`", "Reduce: #{@reduce}"
end

Instance Method Details

#respace_file(file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/spacerace.rb', line 26

def respace_file(file)
  original = "#{file}.original"
  FileUtils.copy file, original

  out_file = File.open file, "w"

  File.foreach original do |line|
    out_file.write respace_line(line)
    # puts respace_line(line, @space, @replacement, @reduce)
  end

  out_file.close
  FileUtils.rm(original) if @no_backup
end

#respace_line(line) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/spacerace.rb', line 15

def respace_line(line)
  regex = /^#{@space}*/
  white_space = line.scan(regex).first
  if (white_space)
    amount = white_space.length
    line.gsub(regex, padd((amount.to_i/@reduce.to_i).to_i, @replacement))
  else
    line
  end
end