How can I work with UTF-16LE source code in Ruby?

I have the following file called test.rbencoding inUTF-16LE

# encoding: UTF-16LE

test = "test!"
p test

Running with the following command fails

ruby ./test.rb

What am I missing here?


In case someone is wondering, the reason I'm trying to install the source code for the UTF-16LE encoding is because I work with the input and output encodings of UTF-16LE. My impression is that if I configure the encoding correctly when I read it in the file and configure the encoding correctly when exiting, and I set it correctly # encoding:in my source, everything should work. If anyone sees something wrong with this (or in a simpler way), feel free to let me know.

+3
1

UTF-16 UTF-16 , , .:)

Ruby 1.9 , - .

, :

s = ''
File.open('utf16le.txt', 'rb:UTF-16LE') do |f| # here you set the encoding
  s = f.read
end
p s.encoding
#=> #<Encoding:UTF-16LE>
p s.length
#=> 19
p s
#=> "test\nmladen\n\u0436\u045F\u0446\u0432\u0431\n\n"

1.9 IO:

http://ruby-doc.org/ruby-1.9/classes/IO.html

+7

Source: https://habr.com/ru/post/1776002/


All Articles