class SpaceRace

Constants

VERSION

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

respace_file(file) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

padd(amount, string) click to toggle source
# File lib/spacerace.rb, line 43
def padd(amount, string)
  padded = ""; amount.times { padded << string }; padded
end