Skip to content

Commit 35e422d

Browse files
authored
Merge pull request #2378 from KitaitiMakoto/io-read-write
Make IO.binread, IO.binwrite, IO.read and IO.write accept _ToPath
2 parents cc9e9c4 + 633ebb9 commit 35e422d

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

core/io.rbs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,7 +2278,7 @@ class IO < Object
22782278
# potential security vulnerabilities if called with untrusted input; see
22792279
# [Command Injection](rdoc-ref:command_injection.rdoc).
22802280
#
2281-
def self.binread: (String name, ?Integer? length, ?Integer offset) -> String
2281+
def self.binread: (path name, ?Integer? length, ?Integer offset) -> String
22822282

22832283
# <!--
22842284
# rdoc-file=io.c
@@ -2291,7 +2291,7 @@ class IO < Object
22912291
# potential security vulnerabilities if called with untrusted input; see
22922292
# [Command Injection](rdoc-ref:command_injection.rdoc).
22932293
#
2294-
def self.binwrite: (String name, _ToS string, ?Integer offset, ?mode: String mode) -> Integer
2294+
def self.binwrite: (path name, _ToS string, ?Integer offset, ?mode: String mode) -> Integer
22952295

22962296
# <!--
22972297
# rdoc-file=io.c
@@ -2716,7 +2716,7 @@ class IO < Object
27162716
# * [Open Options](rdoc-ref:IO@Open+Options).
27172717
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
27182718
#
2719-
def self.read: (String name, ?Integer? length, ?Integer offset, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> String
2719+
def self.read: (path name, ?Integer? length, ?Integer offset, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> String
27202720

27212721
# <!--
27222722
# rdoc-file=io.c
@@ -2986,7 +2986,7 @@ class IO < Object
29862986
# * [Open Options](rdoc-ref:IO@Open+Options).
29872987
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
29882988
#
2989-
def self.write: (String path, _ToS data, ?Integer offset, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> Integer
2989+
def self.write: (path path, _ToS data, ?Integer offset, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> Integer
29902990

29912991
# <!--
29922992
# rdoc-file=io.c

test/stdlib/IO_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class IOSingletonTest < Test::Unit::TestCase
1111
def test_binread
1212
assert_send_type "(String) -> String",
1313
IO, :binread, File.expand_path(__FILE__)
14+
assert_send_type "(Pathname) -> String",
15+
IO, :binread, Pathname(File.expand_path(__FILE__))
1416
assert_send_type "(String, Integer) -> String",
1517
IO, :binread, File.expand_path(__FILE__), 3
1618
assert_send_type "(String, Integer, Integer) -> String",
@@ -26,6 +28,8 @@ def test_binwrite
2628

2729
assert_send_type "(String, String) -> Integer",
2830
IO, :binwrite, filename, content
31+
assert_send_type "(Pathname, String) -> Integer",
32+
IO, :binwrite, Pathname(filename), content
2933
assert_send_type "(String, String, Integer) -> Integer",
3034
IO, :binwrite, filename, content, 0
3135
assert_send_type "(String, String, mode: String) -> Integer",
@@ -35,6 +39,35 @@ def test_binwrite
3539
end
3640
end
3741

42+
def test_read
43+
assert_send_type "(String) -> String",
44+
IO, :read, File.expand_path(__FILE__)
45+
assert_send_type "(Pathname) -> String",
46+
IO, :read, Pathname(File.expand_path(__FILE__))
47+
assert_send_type "(String, Integer) -> String",
48+
IO, :read, File.expand_path(__FILE__), 3
49+
assert_send_type "(String, Integer, Integer) -> String",
50+
IO, :read, File.expand_path(__FILE__), 3, 0
51+
end
52+
53+
def test_write
54+
Dir.mktmpdir do |dir|
55+
filename = File.join(dir, "some_file")
56+
content = "foo"
57+
58+
assert_send_type "(String, String) -> Integer",
59+
IO, :write, filename, content
60+
assert_send_type "(Pathname, String) -> Integer",
61+
IO, :write, Pathname(filename), content
62+
assert_send_type "(String, String, Integer) -> Integer",
63+
IO, :write, filename, content, 0
64+
assert_send_type "(String, String, mode: String) -> Integer",
65+
IO, :write, filename, content, mode: "a"
66+
assert_send_type "(String, String, Integer, mode: String) -> Integer",
67+
IO, :write, filename, content, 0, mode: "a"
68+
end
69+
end
70+
3871
def test_open
3972
Dir.mktmpdir do |dir|
4073
assert_send_type "(Integer) -> IO",

0 commit comments

Comments
 (0)