Repository navigation
Accept an Integer mode and open options in StringIO.new and StringIO.open - #3329
Open
hasannadeem wants to merge 1 commit into
Open
hasannadeem wants to merge 1 commit into
hasannadeem wants to merge 1 commit into
Conversation
…open StringIO.new takes the mode as a String or an Integer of File flags, and the same mode and encoding keywords as IO.new (mode:, flags:, external_encoding:, internal_encoding:, encoding:, textmode:, binmode:). The string and the mode also go through to_str and to_int. The signatures only allowed a String and a String mode, so StringIO.new(str, File::RDONLY) and StringIO.new(str, mode: "rb") were rejected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StringIO.newandStringIO.opentake the mode as a String or as an Integer ofFileflags, and they take the same mode and encoding keywords asIO.new. The signatures only allowed(?String string, ?String? mode), so these were rejected:strio_inithands the mode and the keywords torb_io_extract_modeenc. That function readsmode:,flags:,external_encoding:,internal_encoding:,encoding:,textmode:andbinmode:, and the signature now lists those. The string goes throughStringValueand an Integer mode throughto_int, so the positional arguments arestringandstring | int | nil.The encoding keywords take effect only when a positional mode is given as well.
strio_inituses the string's own encoding when the string is the only positional argument. For example,StringIO.new(s, "r", encoding: "BINARY")reads binary, whileStringIO.new(s, encoding: "BINARY")keepss's encoding.Not changed
StringIO.new(nil)is also accepted at runtime, and then#stringreturnsnil. Allowingnilhere would mean#stringhas to returnString?, which affects every caller, so I left it out.Checked
test/stdlib/StringIO_test.rbpasses on both. With the old signature,test_newandtest_openfail.mode: :rand an unknown keyword, are still rejected.rbs -r stringio validatepasses, andrbs annotatemakes no further changes to the file.