Skip to content

Commit d1b2bf3

Browse files
authored
Merge pull request #2212 from ParadoxV5/chomp
`#gets`s, `#readline`s and `#readlines`s *also* accept `chomp: true`
2 parents 3a74376 + 5f398df commit d1b2bf3

File tree

8 files changed

+68
-8
lines changed

8 files changed

+68
-8
lines changed

Rakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ end
112112

113113
task :stdlib_test => :compile do
114114
test_files = FileList["test/stdlib/**/*_test.rb"].reject do |path|
115-
path =~ %r{Ractor} || path =~ %r{Encoding}
115+
path =~ %r{Ractor} || path =~ %r{Encoding} || path =~ %r{CGI_test}
116116
end
117117

118118
if ENV["RANDOMIZE_STDLIB_TEST_ORDER"] == "true"
@@ -121,6 +121,7 @@ task :stdlib_test => :compile do
121121

122122
sh "#{ruby} -Ilib #{bin}/test_runner.rb #{test_files.join(' ')}"
123123
# TODO: Ractor tests need to be run in a separate process
124+
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/CGI_test.rb"
124125
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/Ractor_test.rb"
125126
sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/Encoding_test.rb"
126127
end

core/kernel.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ module Kernel : BasicObject
11631163
# The style of programming using `$_` as an implicit parameter is gradually
11641164
# losing favor in the Ruby community.
11651165
#
1166-
def self?.gets: (?String arg0, ?Integer arg1) -> String?
1166+
def self?.gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?
11671167

11681168
# <!--
11691169
# rdoc-file=eval.c
@@ -1486,7 +1486,7 @@ module Kernel : BasicObject
14861486
# Optional keyword argument `chomp` specifies whether line separators are to be
14871487
# omitted.
14881488
#
1489-
def self?.readline: (?String arg0, ?Integer arg1) -> String
1489+
def self?.readline: (?String arg0, ?Integer arg1, ?chomp: boolish) -> String
14901490

14911491
# <!--
14921492
# rdoc-file=io.c

core/rbs/unnamed/argf.rbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ module RBS
639639
# See IO.readlines for details about getline_args.
640640
#
641641
%a{annotate:rdoc:copy:ARGF#gets}
642-
def gets: (?String sep, ?Integer limit) -> String?
642+
def gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?
643643

644644
# <!--
645645
# rdoc-file=io.c
@@ -1024,7 +1024,7 @@ module RBS
10241024
# An EOFError is raised at the end of the file.
10251025
#
10261026
%a{annotate:rdoc:copy:ARGF#readline}
1027-
def readline: (?String sep, ?Integer limit) -> String
1027+
def readline: (?String sep, ?Integer limit, ?chomp: boolish) -> String
10281028

10291029
# <!--
10301030
# rdoc-file=io.c
@@ -1044,7 +1044,7 @@ module RBS
10441044
# See `IO.readlines` for a full description of all options.
10451045
#
10461046
%a{annotate:rdoc:copy:ARGF#readlines}
1047-
def readlines: (?String sep, ?Integer limit) -> ::Array[String]
1047+
def readlines: (?String sep, ?Integer limit, ?chomp: boolish) -> ::Array[String]
10481048

10491049
# <!--
10501050
# rdoc-file=io.c

stdlib/openssl/0/openssl.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ module OpenSSL
20252025
#
20262026
# Unlike IO#gets the separator must be provided if a limit is provided.
20272027
#
2028-
def gets: (?String | Regexp eol, ?Integer limit) -> String?
2028+
def gets: (?String | Regexp eol, ?Integer limit, ?chomp: boolish) -> String?
20292029

20302030
# <!--
20312031
# rdoc-file=ext/openssl/lib/openssl/buffering.rb

stdlib/stringio/0/stringio.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class StringIO
338338

339339
def readchar: () -> String
340340

341-
def readline: (?String sep, ?Integer limit) -> String
341+
def readline: (?String sep, ?Integer limit, ?chomp: boolish) -> String
342342

343343
# <!--
344344
# rdoc-file=ext/stringio/stringio.c

test/stdlib/ARGF_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def test_gets
2727
ARGF.class.new(__FILE__), :gets, "\n"
2828
assert_send_type "(::String sep, ::Integer limit) -> ::String",
2929
ARGF.class.new(__FILE__), :gets, "\n", 1
30+
assert_send_type "(chomp: boolish) -> ::String",
31+
ARGF.class.new(__FILE__), :gets, chomp: true
32+
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::String",
33+
ARGF.class.new(__FILE__), :gets, "\n", 1, chomp: true
3034
assert_send_type "() -> nil",
3135
ARGF.class.new(Tempfile.new), :gets
3236
end
@@ -60,6 +64,10 @@ def test_readline
6064
ARGF.class.new(__FILE__), :readline, "\n"
6165
assert_send_type "(::String sep, ::Integer limit) -> ::String",
6266
ARGF.class.new(__FILE__), :readline, "\n", 1
67+
assert_send_type "(chomp: boolish) -> ::String",
68+
ARGF.class.new(__FILE__), :readline, chomp: true
69+
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::String",
70+
ARGF.class.new(__FILE__), :readline, "\n", 1, chomp: true
6371
end
6472

6573
def test_readlines
@@ -69,6 +77,10 @@ def test_readlines
6977
ARGF.class.new(__FILE__), :readlines, "\n"
7078
assert_send_type "(::String sep, ::Integer limit) -> ::Array[::String]",
7179
ARGF.class.new(__FILE__), :readlines, "\n", 1
80+
assert_send_type "(chomp: boolish) -> ::Array[::String]",
81+
ARGF.class.new(__FILE__), :readlines, chomp: true
82+
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::Array[::String]",
83+
ARGF.class.new(__FILE__), :readlines, "\n", 1, chomp: true
7284
end
7385

7486
def test_inspect

test/stdlib/Kernel_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,4 +561,25 @@ def test_raise
561561
end
562562
end
563563
end
564+
565+
def test_readlines
566+
$stdin = File.open(__FILE__)
567+
568+
assert_send_type(
569+
"() -> Array[String]",
570+
JustKernel.new, :readlines
571+
)
572+
573+
with_int(3) do |limit|
574+
with_string(",") do |separator|
575+
$stdin = File.open(__FILE__)
576+
assert_send_type(
577+
"(string, int, chomp: bool) -> Array[String]",
578+
JustKernel.new, :readlines, ",", 3, chomp: true
579+
)
580+
end
581+
end
582+
ensure
583+
$stdin = STDIN
584+
end
564585
end

test/stdlib/StringIO_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,30 @@ def test_truncate
5959
io, :truncate, 10
6060
)
6161
end
62+
63+
def test_readline
64+
assert_send_type "() -> ::String",
65+
StringIO.new("\n"), :readline
66+
assert_send_type "(::String sep) -> ::String",
67+
StringIO.new("\n"), :readline, "\n"
68+
assert_send_type "(::String sep, ::Integer limit) -> ::String",
69+
StringIO.new("\n"), :readline, "\n", 1
70+
assert_send_type "(chomp: boolish) -> ::String",
71+
StringIO.new("\n"), :readline, chomp: true
72+
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::String",
73+
StringIO.new("\n"), :readline, "\n", 1, chomp: true
74+
end
75+
76+
def test_readlines
77+
assert_send_type "() -> ::Array[::String]",
78+
StringIO.new("\n"), :readlines
79+
assert_send_type "(::String sep) -> ::Array[::String]",
80+
StringIO.new("\n"), :readlines, "\n"
81+
assert_send_type "(::String sep, ::Integer limit) -> ::Array[::String]",
82+
StringIO.new("\n"), :readlines, "\n", 1
83+
assert_send_type "(chomp: boolish) -> ::Array[::String]",
84+
StringIO.new("\n"), :readlines, chomp: true
85+
assert_send_type "(::String sep, ::Integer limit, chomp: boolish) -> ::Array[::String]",
86+
StringIO.new("\n"), :readlines, "\n", 1, chomp: true
87+
end
6288
end

0 commit comments

Comments
 (0)