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

Improve IO.binread and IO.binwrite signatures #520

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion core/io.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,20 @@ class IO < Object

def write: (*_ToS arg0) -> Integer

# Opens the file, optionally seeks to the given *offset*, then returns *length*
# bytes (defaulting to the rest of the file). #binread ensures the file is
# closed before returning. The open mode would be `"rb:ASCII-8BIT"`.
#
# IO.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
# IO.binread("testfile", 20) #=> "This is line one\nThi"
# IO.binread("testfile", 20, 10) #=> "ne one\nThis is line "
#
def self.binread: (String name, ?Integer length, ?Integer offset) -> String

def self.binwrite: (String name, _ToS arg0, ?Integer offset, ?external_encoding: String external_encoding, ?internal_encoding: String internal_encoding, ?encoding: String encoding, ?textmode: untyped textmode, ?binmode: untyped binmode, ?autoclose: untyped autoclose, ?mode: String mode) -> Integer
# Same as IO.write except opening the file in binary mode and ASCII-8BIT
# encoding (`"wb:ASCII-8BIT"`).
#
def self.binwrite: (String name, _ToS string, ?Integer offset, ?mode: String mode) -> Integer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] About the argument changes of .binwrite:

  • I remove optional arguments (open_args) except mode: because it is a misuse to specify encoding:, textmode: or autoclose: etc. for this method. But this removal is potentially breaking so I’m ready to revert it if you want.
  • More readable argument name: arg0 -> string (from the API doc)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable. Nice catch! 👏


def self.copy_stream: (_Reader src, _Writer dst, ?Integer copy_length, ?Integer src_offset) -> Integer

Expand Down
32 changes: 32 additions & 0 deletions test/stdlib/IO_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative "test_helper"

class IOSingletonTest < Minitest::Test
include TypeAssertions

testing "singleton(::IO)"

def test_binread
assert_send_type "(String) -> String",
IO, :binread, __FILE__
assert_send_type "(String, Integer) -> String",
IO, :binread, __FILE__, 3
assert_send_type "(String, Integer, Integer) -> String",
IO, :binread, __FILE__, 3, 0
end

def test_binwrite
Dir.mktmpdir do |dir|
filename = File.join(dir, "some_file")
content = "foo"

assert_send_type "(String, String) -> Integer",
IO, :binwrite, filename, content
assert_send_type "(String, String, Integer) -> Integer",
IO, :binwrite, filename, content, 0
assert_send_type "(String, String, mode: String) -> Integer",
IO, :binwrite, filename, content, mode: "a"
assert_send_type "(String, String, Integer, mode: String) -> Integer",
IO, :binwrite, filename, content, 0, mode: "a"
end
end
end