Class: Codes21
- Inherits:
-
Object
- Object
- Codes21
- Defined in:
- lib/codes21.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get_codes(path, suffixes, coding = "UTF-8") ⇒ Object
- #write_result(result_file_path, coding = "UTF-8") ⇒ Object
Class Method Details
.run(path, suffixes, result_file_path, input_coding = "UTF-8", output_coding = "UTF-8") ⇒ Object
38 39 40 41 42 |
# File 'lib/codes21.rb', line 38 def self.run(path, suffixes, result_file_path, input_coding="UTF-8", output_coding="UTF-8") codes21 = Codes21.new codes21.get_codes(path, suffixes, input_coding) codes21.write_result(result_file_path, output_coding) end |
Instance Method Details
#get_codes(path, suffixes, coding = "UTF-8") ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/codes21.rb', line 2 def get_codes(path, suffixes, coding="UTF-8") @items ||= [] Dir.glob("#{path}/**/*.{#{suffixes}}").each do |file| title = file.split('/').last.force_encoding(coding) puts file puts '*' * 70 codes = File.read(file).force_encoding(coding) # sb的软件著作权要求不能有空行 codes = codes.squeeze("\n") puts codes item = {title: title, codes: codes} @items << item end @items end |
#write_result(result_file_path, coding = "UTF-8") ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/codes21.rb', line 20 def write_result(result_file_path, coding="UTF-8") File.open(result_file_path, 'w') do |f| @items.each do |item| title = "File:" + item[:title] code_label = "Code:" codes = item[:codes] content = title + "\n" + code_label + "\n" + codes + "\n" content = content.squeeze("\n") f.print content.encode(coding) end end puts '*' * 70 puts "codes files count: #{@items.size}" end |