Skip to content

Commit

Permalink
Fix IO#gets type
Browse files Browse the repository at this point in the history
- Can take a string as a `nil` separator
- Can take limit integer as a first argument without a separator.
- Can take an optional keyword argument `chomp`
  • Loading branch information
kateinoigakukun committed Nov 17, 2023
1 parent c4dd6ed commit c384900
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/io.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,8 @@ class IO < Object
# f.gets(chomp: true) # => nil
# f.close
#
def gets: (?String sep, ?Integer limit) -> String?
def gets: (string? sep, ?int limit, ?chomp: boolish) -> String?
| (?int limit, ?chomp: boolish) -> String?

# <!--
# rdoc-file=io.c
Expand Down
25 changes: 25 additions & 0 deletions test/stdlib/IO_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,31 @@ def test_sync
io, :sync
end
end

def test_gets
IO.open(IO.sysopen(File.expand_path(__FILE__))) do |io|
assert_send_type '() -> String',
io, :gets
assert_send_type '(String) -> String',
io, :gets, ""
assert_send_type '(Integer) -> String',
io, :gets, 10
assert_send_type '(chomp: bool) -> String',
io, :gets, chomp: true

assert_send_type '(Integer, chomp: bool) -> String',
io, :gets, 42, chomp: true
assert_send_type '(String, Integer, chomp: bool) -> String',
io, :gets, "", 42, chomp: true
assert_send_type '(String, chomp: bool) -> String',
io, :gets, "", chomp: true

assert_send_type '(nil) -> String',
io, :gets, nil
assert_send_type '(nil, chomp: bool) -> nil',
io, :gets, nil, chomp: true
end
end
end

class IOWaitTest < Test::Unit::TestCase
Expand Down

0 comments on commit c384900

Please sign in to comment.