Class: Apstrings::Reader
- Inherits:
-
Object
- Object
- Apstrings::Reader
- Defined in:
- lib/apstrings/reader.rb
Overview
Class for reading in files and returning an array of its content
Class Method Summary collapse
-
.read(file) ⇒ Object
Reads in a file and returns an array consisting of each line of input cleaned of new line characters.
Class Method Details
.read(file) ⇒ Object
Reads in a file and returns an array consisting of each line of input cleaned of new line characters
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/apstrings/reader.rb', line 7 def self.read(file) File.open(file, 'r') do |f| str = f.read if str.bytesize >= 3 and str.getbyte(0) == 0xFF and str.getbyte(1) == 0xFE #UTF-16lE encoder = Encoding::Converter.new("UTF-16lE", "UTF-8") str = encoder.convert(str) str = str.byteslice(3, str.bytesize - 3) # 去掉FFFE elsif str.bytesize >= 3 and str.getbyte(0) == 0xFE and str.getbyte(1) == 0xFF #UTF-16bE encoder = Encoding::Converter.new("UTF-16bE", "UTF-8") str = encoder.convert(str) str = str.byteslice(3, str.bytesize - 3) # 去掉FEFF elsif str.bytesize >= 4 and str.getbyte(0) == 0xEF and str.getbyte(1) == 0xBB and str.getbyte(2) == 0xBF #UTF8 str = str.byteslice(4, str.bytesize - 4) # 去掉EFBBBF else #ASCII str = str.force_encoding('UTF-8') end content = str content.each_line.inject([]) do |content_array, line| line.gsub!("\n","") content_array.push(line) end end end |