Skip to content

Commit 5d0bde3

Browse files
authored
Merge pull request #1674 from ruby/ruby-33
Update methods for Ruby 3.3
2 parents 18a28c5 + 95cdfa9 commit 5d0bde3

19 files changed

Lines changed: 729 additions & 8 deletions

.github/workflows/ruby.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ jobs:
3131
job: stdlib_test
3232
- container_tag: 3.1-dev-focal
3333
job: stdlib_test
34-
include:
35-
- container_tag: master-nightly-focal
36-
allow_failures: "true"
34+
- container_tag: 3.2-dev-focal
35+
job: stdlib_test
3736
container:
3837
image: rubylang/ruby:${{ matrix.container_tag }}
3938
steps:

core/dir.rbs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,61 @@ class Dir
364364
#
365365
def self.exist?: (path | io file_name) -> bool
366366

367+
# <!--
368+
# rdoc-file=dir.c
369+
# - Dir.fchdir(fd) -> 0
370+
# - Dir.fchdir(fd) { ... } -> object
371+
# -->
372+
# Changes the current working directory to the directory specified by the
373+
# integer file descriptor `fd`.
374+
#
375+
# When passing a file descriptor over a UNIX socket or to a child process, using
376+
# `fchdir` instead of `chdir` avoids the [time-of-check to time-of-use
377+
# vulnerability](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use)
378+
#
379+
# With no block, changes to the directory given by `fd`:
380+
#
381+
# Dir.chdir('/var/spool/mail')
382+
# Dir.pwd # => "/var/spool/mail"
383+
# dir = Dir.new('/usr')
384+
# fd = dir.fileno
385+
# Dir.fchdir(fd) do
386+
# Dir.pwd # => "/usr"
387+
# end
388+
# Dir.pwd # => "/var/spool/mail"
389+
#
390+
# With a block, temporarily changes the working directory:
391+
#
392+
# * Calls the block with the argument.
393+
# * Changes to the given directory.
394+
# * Executes the block
395+
# * Restores the previous working directory.
396+
# * Returns the block's return value.
397+
#
398+
#
399+
# Example:
400+
#
401+
# Dir.chdir('/var/spool/mail')
402+
# Dir.pwd # => "/var/spool/mail"
403+
# Dir.chdir('/tmp') do
404+
# Dir.pwd # => "/tmp"
405+
# end
406+
# Dir.pwd # => "/var/spool/mail"
407+
#
408+
# This method uses the
409+
# [fchdir()](https://www.man7.org/linux/man-pages/man3/fchdir.3p.html) function
410+
# defined by POSIX 2008; the method is not implemented on non-POSIX platforms
411+
# (raises NotImplementedError).
412+
#
413+
# Raises an exception if the file descriptor is not valid.
414+
#
415+
# In a multi-threaded program an error is raised if a thread attempts to open a
416+
# `chdir` block while another thread has one open, or a call to `chdir` without
417+
# a block occurs inside a block passed to `chdir` (even in the same thread).
418+
#
419+
def self.fchdir: (int) -> Integer
420+
| [T] (int) { () -> T } -> T
421+
367422
# <!--
368423
# rdoc-file=dir.c
369424
# - Dir.foreach(dirpath, encoding: 'UTF-8') {|entry_name| ... } -> nil
@@ -397,6 +452,28 @@ class Dir
397452
#
398453
alias self.foreach self.each_child
399454

455+
# <!--
456+
# rdoc-file=dir.c
457+
# - Dir.for_fd(fd) -> dir
458+
# -->
459+
# Returns a new Dir object representing the directory specified by the given
460+
# integer directory file descriptor `fd`:
461+
#
462+
# d0 = Dir.new('..')
463+
# d1 = Dir.for_fd(d0.fileno)
464+
#
465+
# Note that the returned `d1` does not have an associated path:
466+
#
467+
# d0.path # => '..'
468+
# d1.path # => nil
469+
#
470+
# This method uses the
471+
# [fdopendir()](https://www.man7.org/linux/man-pages/man3/fdopendir.3p.html)
472+
# function defined by POSIX 2008; the method is not implemented on non-POSIX
473+
# platforms (raises NotImplementedError).
474+
#
475+
def self.for_fd: (int) -> Dir
476+
400477
# <!--
401478
# rdoc-file=dir.c
402479
# - Dir.pwd -> string
@@ -678,6 +755,20 @@ class Dir
678755

679756
public
680757

758+
# <!--
759+
# rdoc-file=dir.c
760+
# - chdir -> nil
761+
# -->
762+
# Changes the current working directory to the path of `self`:
763+
#
764+
# Dir.pwd # => "/"
765+
# dir = Dir.new('example')
766+
# dir.chdir
767+
# dir.pwd # => "/example"
768+
#
769+
def chdir: () -> Integer
770+
| [T] { () -> T } -> T
771+
681772
# <!--
682773
# rdoc-file=dir.c
683774
# - children -> array

core/fiber.rbs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,20 @@ class Fiber < Object
369369
#
370370
alias inspect to_s
371371

372+
# <!--
373+
# rdoc-file=cont.c
374+
# - fiber.kill -> nil
375+
# -->
376+
# Terminates `fiber` by raising an uncatchable exception, returning the
377+
# terminated Fiber.
378+
#
379+
# If the fiber has not been started, transition directly to the terminated
380+
# state.
381+
#
382+
# If the fiber is already terminated, does nothing.
383+
#
384+
def kill: () -> nil
385+
372386
# <!--
373387
# rdoc-file=cont.c
374388
# - fiber.raise -> obj

core/io/buffer.rbs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ class IO
165165
#
166166
def self.map: (File file, ?Integer? size, ?Integer offset, ?Integer flags) -> Buffer
167167

168+
# <!--
169+
# rdoc-file=io_buffer.c
170+
# - IO::Buffer.string(length) {|io_buffer| ... read/write io_buffer ...} -> string
171+
# -->
172+
# Creates a new string of the given length and yields a IO::Buffer instance to
173+
# the block which uses the string as a source. The block is expected to write to
174+
# the buffer and the string will be returned.
175+
#
176+
# IO::Buffer.string(4) do |buffer|
177+
# buffer.set_string("Ruby")
178+
# end
179+
# # => "Ruby"
180+
#
181+
def self.string: (int) { (Buffer) -> void } -> String
182+
168183
public
169184

170185
# <!--

core/match_data.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ class MatchData
305305
# m.named_captures(symbolize_names: true) #=> {:a => "1"}
306306
#
307307
def named_captures: () -> Hash[String, String?]
308+
| (symbolize_names: true) -> Hash[Symbol, String?]
309+
| (symbolize_names: boolish) -> Hash[String | Symbol, String?]
308310

309311
# <!--
310312
# rdoc-file=re.c

core/module.rbs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ class Module < Object
14631463
#
14641464
# Returns a module, where refined methods are defined.
14651465
#
1466-
def refine: (Module mod) { () -> void } -> Refinement
1466+
def refine: (Module mod) { () [self: Refinement] -> void } -> Refinement
14671467

14681468
# <!--
14691469
# rdoc-file=eval.c
@@ -1527,6 +1527,50 @@ class Module < Object
15271527
#
15281528
def remove_method: (*interned arg0) -> self
15291529

1530+
# <!--
1531+
# rdoc-file=object.c
1532+
# - mod.set_temporary_name(string) -> self
1533+
# - mod.set_temporary_name(nil) -> self
1534+
# -->
1535+
# Sets the temporary name of the module `mod`. This name is used as a prefix for
1536+
# the names of constants declared in `mod`. If the module is assigned a
1537+
# permanent name, the temporary name is discarded.
1538+
#
1539+
# After a permanent name is assigned, a temporary name can no longer be set, and
1540+
# this method raises a RuntimeError.
1541+
#
1542+
# If the name given is not a string or is a zero length string, this method
1543+
# raises an ArgumentError.
1544+
#
1545+
# The temporary name must not be a valid constant name, to avoid confusion with
1546+
# actual constants. If you attempt to set a temporary name that is a a valid
1547+
# constant name, this method raises an ArgumentError.
1548+
#
1549+
# If the given name is `nil`, the module becomes anonymous.
1550+
#
1551+
# Example:
1552+
#
1553+
# m = Module.new # => #<Module:0x0000000102c68f38>
1554+
# m.name #=> nil
1555+
#
1556+
# m.set_temporary_name("fake_name") # => fake_name
1557+
# m.name #=> "fake_name"
1558+
#
1559+
# m.set_temporary_name(nil) # => #<Module:0x0000000102c68f38>
1560+
# m.name #=> nil
1561+
#
1562+
# n = Module.new
1563+
# n.set_temporary_name("fake_name")
1564+
#
1565+
# n::M = m
1566+
# n::M.name #=> "fake_name::M"
1567+
# N = n
1568+
#
1569+
# N.name #=> "nested_fake_name"
1570+
# N::M.name #=> "N::M"
1571+
#
1572+
def set_temporary_name: (string?) -> self
1573+
15301574
# <!--
15311575
# rdoc-file=object.c
15321576
# - mod.singleton_class? -> true or false

core/object_space/weak_key_map.rbs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
%a{annotate:rdoc:skip}
2+
module ObjectSpace
3+
# <!-- rdoc-file=weakmap.c -->
4+
# An ObjectSpace::WeakKeyMap object holds references to any objects, but objects
5+
# uses as keys can be garbage collected.
6+
#
7+
# Objects used as values can't be garbage collected until the key is.
8+
#
9+
class WeakKeyMap[Key, Value]
10+
public
11+
12+
# <!--
13+
# rdoc-file=weakmap.c
14+
# - map[key] -> value
15+
# -->
16+
# Returns the value associated with the given `key` if found.
17+
#
18+
# If `key` is not found, returns `nil`.
19+
#
20+
def []: (Key) -> Value?
21+
22+
# <!--
23+
# rdoc-file=weakmap.c
24+
# - map[key] = value -> value
25+
# -->
26+
# Associates the given `value` with the given `key`; returns `value`.
27+
#
28+
# The reference to `key` is weak, so when there is no other reference to `key`
29+
# it may be garbage collected.
30+
#
31+
# If the given `key` exists, replaces its value with the given `value`; the
32+
# ordering is not affected
33+
#
34+
def []=: (Key, Value?) -> Value?
35+
36+
# <!--
37+
# rdoc-file=weakmap.c
38+
# - map.clear -> self
39+
# -->
40+
# Removes all map entries; returns `self`.
41+
#
42+
def clear: () -> self
43+
44+
# <!--
45+
# rdoc-file=weakmap.c
46+
# - map.delete(key) -> value or nil
47+
# - map.delete(key) {|key| ... } -> object
48+
# -->
49+
# Deletes the entry for the given `key` and returns its associated value.
50+
#
51+
# If no block is given and `key` is found, deletes the entry and returns the
52+
# associated value:
53+
# m = ObjectSpace::WeakKeyMap.new
54+
# m["foo"] = 1
55+
# m.delete("foo") # => 1
56+
# m["foo"] # => nil
57+
#
58+
# If no block given and `key` is not found, returns `nil`.
59+
#
60+
# If a block is given and `key` is found, ignores the block, deletes the entry,
61+
# and returns the associated value:
62+
# m = ObjectSpace::WeakKeyMap.new
63+
# m["foo"] = 2
64+
# h.delete("foo") { |key| raise 'Will never happen'} # => 2
65+
#
66+
# If a block is given and `key` is not found, calls the block and returns the
67+
# block's return value:
68+
# m = ObjectSpace::WeakKeyMap.new
69+
# h.delete("nosuch") { |key| "Key #{key} not found" } # => "Key nosuch not found"
70+
#
71+
def delete: (Key) -> Value?
72+
| [T] (Key) { (Key) -> T } -> (Value | T)
73+
74+
# <!--
75+
# rdoc-file=weakmap.c
76+
# - map.getkey(key) -> existing_key or nil
77+
# -->
78+
# Returns the existing equal key if it exists, otherwise returns `nil`.
79+
#
80+
def getkey: (untyped) -> Key?
81+
82+
# <!--
83+
# rdoc-file=weakmap.c
84+
# - map.inspect -> new_string
85+
# -->
86+
# Returns a new String containing informations about the map:
87+
#
88+
# m = ObjectSpace::WeakKeyMap.new
89+
# m[key] = value
90+
# m.inspect # => "#<ObjectSpace::WeakKeyMap:0x00000001028dcba8 size=1>"
91+
#
92+
def inspect: () -> String
93+
94+
# <!--
95+
# rdoc-file=weakmap.c
96+
# - hash.key?(key) -> true or false
97+
# -->
98+
# Returns `true` if `key` is a key in `self`, otherwise `false`.
99+
#
100+
def key?: (Key) -> bool
101+
end
102+
end

core/process.rbs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,33 @@ module Process
14211421
# Process.waitpid2 is an alias for Process.waitpid.
14221422
#
14231423
def self.waitpid2: (?Integer pid, ?Integer flags) -> [ Integer, Process::Status ]
1424+
1425+
# <!--
1426+
# rdoc-file=process.c
1427+
# - Process.warmup -> true
1428+
# -->
1429+
# Notify the Ruby virtual machine that the boot sequence is finished, and that
1430+
# now is a good time to optimize the application. This is useful for long
1431+
# running applications.
1432+
#
1433+
# This method is expected to be called at the end of the application boot. If
1434+
# the application is deployed using a pre-forking model, `Process.warmup` should
1435+
# be called in the original process before the first fork.
1436+
#
1437+
# The actual optimizations performed are entirely implementation specific and
1438+
# may change in the future without notice.
1439+
#
1440+
# On CRuby, `Process.warmup`:
1441+
#
1442+
# * Performs a major GC.
1443+
# * Compacts the heap.
1444+
# * Promotes all surviving objects to the old generation.
1445+
# * Precomputes the coderange of all strings.
1446+
# * Frees all empty heap pages and increments the allocatable pages counter by
1447+
# the number of pages freed.
1448+
# * Invoke `malloc_trim` if available to free empty malloc pages.
1449+
#
1450+
def self.warmup: () -> bool
14241451
end
14251452

14261453
# <!-- rdoc-file=process.c -->

core/ractor.rbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,4 +1014,8 @@ class Ractor
10141014

10151015
class UnsafeError < Ractor::Error
10161016
end
1017+
1018+
%a{annotate:rdoc:skip}
1019+
class Selector
1020+
end
10171021
end

0 commit comments

Comments
 (0)