Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringIO.new does not accept an encoding: keyword argument #2793

Closed
postmodern opened this issue Nov 21, 2022 · 3 comments
Closed

StringIO.new does not accept an encoding: keyword argument #2793

postmodern opened this issue Nov 21, 2022 · 3 comments

Comments

@postmodern
Copy link

StringIO.new does not appear to accept keyword arguments.

TruffleRuby 22.3.0

require 'stringio'
StringIO.new(encoding: Encoding::ASCII_8BIT)
<internal:core> core/type.rb:104:in `coerce_to_failed': wrong argument type Hash (expected String) (TypeError)
	from <internal:core> core/type.rb:93:in `execute_coerce_to'
	from <internal:core> core/type.rb:85:in `coerce_to'
	from /opt/rubies/truffleruby-22.3.0/lib/truffle/stringio.rb:146:in `initialize'
	from (irb):1:in `<top (required)>'
	from <internal:core> core/kernel.rb:407:in `loop'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from /opt/rubies/truffleruby-22.3.0/lib/gems/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
	from <internal:core> core/kernel.rb:376:in `load'
	from <internal:core> core/kernel.rb:376:in `load'
	from /opt/rubies/truffleruby-22.3.0/bin/irb:42:in `<main>'
<internal:core> core/type.rb:91:in `execute_coerce_to': undefined method `to_str' for {:encoding=>#<Encoding:ASCII-8BIT>}:Hash (NoMethodError)
Did you mean?  to_s
               to_set
	from <internal:core> core/type.rb:85:in `coerce_to'
	from /opt/rubies/truffleruby-22.3.0/lib/truffle/stringio.rb:146:in `initialize'
	from (irb):1:in `<top (required)>'
	from <internal:core> core/kernel.rb:407:in `loop'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from /opt/rubies/truffleruby-22.3.0/lib/gems/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
	from <internal:core> core/kernel.rb:376:in `load'
	from <internal:core> core/kernel.rb:376:in `load'
	from /opt/rubies/truffleruby-22.3.0/bin/irb:42:in `<main>'
StringIO.new("foo", encoding: Encoding::ASCII_8BIT)
<internal:core> core/type.rb:276:in `convert_type': no implicit conversion of Hash into String (TypeError)
	from <internal:core> core/type.rb:258:in `rb_convert_type'
	from <internal:core> core/kernel.rb:171:in `StringValue'
	from /opt/rubies/truffleruby-22.3.0/lib/truffle/stringio.rb:154:in `initialize'
	from (irb):2:in `<top (required)>'
	from <internal:core> core/kernel.rb:407:in `loop'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from /opt/rubies/truffleruby-22.3.0/lib/gems/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
	from <internal:core> core/kernel.rb:376:in `load'
	from <internal:core> core/kernel.rb:376:in `load'
	from /opt/rubies/truffleruby-22.3.0/bin/irb:42:in `<main>'

CRuby 3.0.4

require 'stringio'
StringIO.new(encoding: Encoding::ASCII_8BIT)
# => #<StringIO:0x00005562ac9f45e8>
StringIO.new("foo", encoding: Encoding::ASCII_8BIT)
# => #<StringIO:0x00005562ac82c6e8>
@flavorjones
Copy link
Contributor

flavorjones commented Nov 21, 2022

It doesn't seem like this option does anything in CRuby. Using 3.1.2:

#! /usr/bin/env ruby

require "stringio"

StringIO.new().string.encoding
# => #<Encoding:UTF-8>

StringIO.new(encoding: Encoding::KOI8_R).string.encoding
# => #<Encoding:UTF-8>

# but:

StringIO.new.tap {|s| s.set_encoding(Encoding::KOI8_R) }.string.encoding
# => #<Encoding:KOI8-R>

StringIO.new(String.new(encoding: Encoding::KOI8_R)).string.encoding
# => #<Encoding:KOI8-R>

Maybe I'm misunderstanding the usage here, I'd love to better understand how this is supposed to work.

@andrykonchin andrykonchin self-assigned this Nov 21, 2022
@andrykonchin
Copy link
Member

andrykonchin commented Nov 21, 2022

Looking at the CRuby implementation of StringIO I assume that StringIO.new accepts the keyword arguments only for compatibility of method signature with IO.new and File.new. It checks some error cases and just ignores values.

Examples of errors:

StringIO.new("", "w", mode: "r")
# (irb):15:in `initialize': mode specified twice (ArgumentError)

StringIO.new("", "w:utf-8", encoding: "ascii-8bit")
# (irb):16:in `initialize': encoding specified twice (ArgumentError)

@andrykonchin
Copy link
Member

Fixed in d4c94d8. Thank you for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants