Skip to content

Instantly share code, notes, and snippets.

@liuxd
Last active September 7, 2017 01:35
Show Gist options
  • Save liuxd/605cc66b406da30b4332 to your computer and use it in GitHub Desktop.
Save liuxd/605cc66b406da30b4332 to your computer and use it in GitHub Desktop.
[Win2unix.rb] Replace the windows' line breaks into unix line breaks.
# -*- encoding : utf-8 -*-
module Win2unix
@@file_list = []
def self.tidy path, ext = 'txt'
get_all_files path, ext
@@file_list.each do |file|
handle file
end
@@file_list.size
end
def self.get_all_files path, ext = 'txt'
Dir.glob("#{path}/*") do |file|
if File.directory? file
get_all_files file
else
if File.extname(file) == '.' + ext
@@file_list.push file
end
end
end
end
def self.handle file
origin = IO.binread file
handled = origin.gsub /\r\n/, "\n"
f = File.new file, 'w'
f.print handled
f.close
end
end
# end of this file.
@liuxd
Copy link
Author

liuxd commented Dec 30, 2015

Usage

require 'win2unix'

Win2unix.tidy(your_path) # the ext parameter is 'txt' as default.

Win2unix.tidy(your_path, 'php')
Win2unix.tidy(your_path, 'txt')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment