Feature #13532

Enable :encoding key or open-uri (open()) similar as to how and File.readlines() already allow for

Added by shevegen (Robert A. Heiler) almost 2 years ago. Updated over 1 year ago.

Target version:


Hello ruby core team and everyone else.

I lately had some odd behaviour and mixing of Encodings in my

I had a better look and realized that open-uri would return
UTF-8 whereas my other scripts would use another encoding. So
far so good, no big deal, I can just change the encoding. But
I was not aware of this.

I then looked at the documentation and found it to be lacking:

Compare this to File.readlines():

File.readlines() also tells you how many arguments can be

(1) So my first suggestion is - could some kind soul please also
enable this for open-uri?

The major reason for me filing this feature request, is about
encoding support though, via open-uri. and File.readlines() both allow an :encoding key
to be passed. This is very nifty, I use it a lot and it works
very well.

open-uri or rather its open() functionality, allows you to
pass in a Hash but this Hash has no :encoding key.


open("", encoding: 'ISO-8859-1')


ArgumentError: unrecognized option: encoding

Could this perhaps be added for symmetry and consistency,
so that open() from open-uri will behave like
and File.readlines() would? E. g. an example for the latter
would be.

File.readlines('foobar.rb', :encoding => 'ISO-8859-1')

The very same syntax could also be used for open() of

Thanks for reading!


Updated by shyouhei (Shyouhei Urabe) almost 2 years ago

  • Assignee set to akr (Akira Tanaka)
  • Status changed from Open to Assigned

FYI bare (non-uri) open has encoding: option.

irb(main):001:0>"tmp.txt", encoding: Encoding::Windows_31J) {|fp| fp.external_encoding }
=> #<Encoding:Windows-31J>

Updated by akr (Akira Tanaka) over 1 year ago

  • Status changed from Assigned to Closed

:encoding option is implemented at r60232.

Note that encoding in mode string is already possible as open(filename, "r:enc").

Also available in: Atom PDF