Skip to content

Commit 57f58fa

Browse files
authored
Merge pull request #2798 from HoneyryderChuck/open-extra-options
add extra kwarg options for File#initialize and derivatives
2 parents 85565e2 + b82a4db commit 57f58fa

4 files changed

Lines changed: 129 additions & 5 deletions

File tree

core/file.rbs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,30 @@ class File < IO
847847
# * [Open Options](rdoc-ref:IO@Open+Options).
848848
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
849849
#
850-
def initialize: (path | int file_name, ?string | int mode, ?int perm) -> void
850+
def initialize: (
851+
path | int file_name,
852+
?string | int mode,
853+
?int perm,
854+
# open options
855+
?mode: Integer | String,
856+
?flags: Integer,
857+
?external_encoding: encoding,
858+
?internal_encoding: encoding,
859+
?encoding: encoding,
860+
?textmode: boolish,
861+
?binmode: boolish,
862+
?autoclose: boolish,
863+
?path: path,
864+
# encoding options
865+
?invalid: :replace | nil,
866+
?undef: :replace | nil,
867+
?replace: String | nil,
868+
?fallback: Hash[string, string] | ^(String) -> string | Method | nil,
869+
?xml: :text | :attr | nil,
870+
?cr_newline: bool,
871+
?crlf_newline: bool,
872+
?universal_newline: bool
873+
) -> void
851874

852875
# <!--
853876
# rdoc-file=file.c
@@ -1407,8 +1430,54 @@ class File < IO
14071430
# With a block given, calls the block with the File object and returns the
14081431
# block's value.
14091432
#
1410-
def self.open: (path | int file_name, ?string | int mode, ?int perm) -> instance
1411-
| [T] (path | int file_name, ?string | int mode, ?int perm) { (File) -> T } -> T
1433+
def self.open: (
1434+
path | int file_name,
1435+
?string | int mode,
1436+
?int perm,
1437+
# open options
1438+
?mode: Integer | String,
1439+
?flags: Integer,
1440+
?external_encoding: encoding,
1441+
?internal_encoding: encoding,
1442+
?encoding: encoding,
1443+
?textmode: boolish,
1444+
?binmode: boolish,
1445+
?autoclose: boolish,
1446+
?path: path,
1447+
# encoding options
1448+
?invalid: :replace | nil,
1449+
?undef: :replace | nil,
1450+
?replace: String | nil,
1451+
?fallback: Hash[string, string] | ^(String) -> string | Method | nil,
1452+
?xml: :text | :attr | nil,
1453+
?cr_newline: bool,
1454+
?crlf_newline: bool,
1455+
?universal_newline: bool
1456+
) -> instance
1457+
| [T] (
1458+
path | int file_name,
1459+
?string | int mode,
1460+
?int perm,
1461+
# open options
1462+
?mode: Integer | String,
1463+
?flags: Integer,
1464+
?external_encoding: encoding,
1465+
?internal_encoding: encoding,
1466+
?encoding: encoding,
1467+
?textmode: boolish,
1468+
?binmode: boolish,
1469+
?autoclose: boolish,
1470+
?path: path,
1471+
# encoding options
1472+
?invalid: :replace | nil,
1473+
?undef: :replace | nil,
1474+
?replace: String | nil,
1475+
?fallback: Hash[string, string] | ^(String) -> string | Method | nil,
1476+
?xml: :text | :attr | nil,
1477+
?cr_newline: bool,
1478+
?crlf_newline: bool,
1479+
?universal_newline: bool
1480+
) { (File) -> T } -> T
14121481

14131482
# <!--
14141483
# rdoc-file=file.c

core/pathname.rbs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,51 @@ class Pathname
838838
# -->
839839
# See `File.open`. Opens the file for reading or writing.
840840
#
841-
def open: (?string | int mode, ?int perm) -> File
842-
| [T] (?string | int mode, ?int perm) { (File) -> T } -> T
841+
def open: (
842+
?string | int mode,
843+
?int perm,
844+
# open options
845+
?flags: Integer,
846+
?external_encoding: encoding,
847+
?internal_encoding: encoding,
848+
?encoding: encoding,
849+
?textmode: boolish,
850+
?binmode: boolish,
851+
?autoclose: boolish,
852+
?path: path,
853+
# encoding options
854+
?invalid: :replace | nil,
855+
?undef: :replace | nil,
856+
?replace: String | nil,
857+
?fallback: Hash[string, string] | ^(String) -> string | Method | nil,
858+
?xml: :text | :attr | nil,
859+
?cr_newline: bool,
860+
?crlf_newline: bool,
861+
?universal_newline: bool
862+
) -> File
863+
| [T] (
864+
?string | int mode,
865+
?int perm,
866+
# open options
867+
?mode: Integer | String,
868+
?flags: Integer,
869+
?external_encoding: encoding,
870+
?internal_encoding: encoding,
871+
?encoding: encoding,
872+
?textmode: boolish,
873+
?binmode: boolish,
874+
?autoclose: boolish,
875+
?path: path,
876+
# encoding options
877+
?invalid: :replace | nil,
878+
?undef: :replace | nil,
879+
?replace: String | nil,
880+
?fallback: Hash[string, string] | ^(String) -> string | Method | nil,
881+
?xml: :text | :attr | nil,
882+
?cr_newline: bool,
883+
?crlf_newline: bool,
884+
?universal_newline: bool
885+
) { (File) -> T } -> T
843886

844887
# <!--
845888
# rdoc-file=pathname_builtin.rb

test/stdlib/File_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def test_new
2929
File, :new, File.expand_path(__FILE__), "r", 0644
3030
assert_send_type "(String, String, ToInt) -> File",
3131
File, :new, File.expand_path(__FILE__), "r", ToInt.new(0644)
32+
assert_send_type "(String, mode: String) -> File",
33+
File, :new, File.expand_path(__FILE__), mode: "r"
34+
assert_send_type "(String, mode: Integer) -> File",
35+
File, :new, File.expand_path(__FILE__), mode: File::RDONLY
3236
end
3337

3438
def test_open
@@ -56,6 +60,11 @@ def test_open
5660
File, :open, File.expand_path(__FILE__), "r", ToInt.new(0644)
5761
assert_send_type "(String) { (File) -> String } -> String",
5862
File, :open, File.expand_path(__FILE__) do |file| file.read end
63+
assert_send_type "(String, mode: String) -> File",
64+
File, :open, File.expand_path(__FILE__), mode: "r"
65+
assert_send_type "(String, mode: Integer) -> File",
66+
File, :open, File.expand_path(__FILE__), mode: File::RDONLY
67+
5968
end
6069

6170
def test_absolute_path

test/stdlib/Pathname_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ def test_open
479479
path, :open, 'r', 0644
480480
assert_send_type '() { (File) -> true} -> true',
481481
path, :open do true end
482+
assert_send_type "(String, binmode: bool) -> File",
483+
path, :open, 'r', binmode: true
484+
482485
end
483486
end
484487

0 commit comments

Comments
 (0)