Does Ruby provide a way to do File.read() with specified encoding?

前端 未结 1 1669
小鲜肉
小鲜肉 2020-12-10 09:56

In ruby 1.9.x, we can specify the encoding with File.open(\'filename\',\'r:iso-8859-1\'). I often prefer to use a one-line File.read() if I am reading many shor

相关标签:
1条回答
  • 2020-12-10 10:39

    From the fine manual:

    read(name, [length [, offset]], open_args) → string

    Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file). read ensures the file is closed before returning.

    If the last argument is a hash, it specifies option for internal open().

    So you can say things like this:

    >> s = File.read('pancakes', :encoding => 'iso-8859-1')
    >> s.encoding
    => #<Encoding:ISO-8859-1>
    
    0 讨论(0)
提交回复
热议问题