Skip to content

Commit 0845b2b

Browse files
authored
Merge pull request #2189 from ruby/3.4-changes
Update type definitions for Ruby 3.4
2 parents f7315ca + ba6d311 commit 0845b2b

31 files changed

+561
-83
lines changed

core/array.rbs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,43 @@ class Array[unchecked out Elem] < Object
17631763
| [T] (int index, T default) -> (Elem | T)
17641764
| [T] (int index) { (int index) -> T } -> (Elem | T)
17651765

1766+
# <!--
1767+
# rdoc-file=array.rb
1768+
# - fetch_values(*indexes) -> new_array
1769+
# - fetch_values(*indexes) {|index| ... } -> new_array
1770+
# -->
1771+
# With no block given, returns a new array containing the elements of `self` at
1772+
# the offsets given by `indexes`; each of the `indexes` must be an
1773+
# [integer-convertible
1774+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects):
1775+
#
1776+
# a = [:foo, :bar, :baz]
1777+
# a.fetch_values(3, 1) # => [:baz, :foo]
1778+
# a.fetch_values(3.1, 1) # => [:baz, :foo]
1779+
# a.fetch_values # => []
1780+
#
1781+
# For a negative index, counts backwards from the end of the array:
1782+
#
1783+
# a.fetch_values([-2, -1]) # [:bar, :baz]
1784+
#
1785+
# When no block is given, raises an exception if any index is out of range.
1786+
#
1787+
# With a block given, for each index:
1788+
#
1789+
# * If the index in in range, uses an element of `self` (as above).
1790+
# * Otherwise calls, the block with the index, and uses the block's return
1791+
# value.
1792+
#
1793+
# Example:
1794+
#
1795+
# a = [:foo, :bar, :baz]
1796+
# a.fetch_values(1, 0, 42, 777) {|index| index.to_s}
1797+
# # => [:bar, :foo, "42", "777"]
1798+
#
1799+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
1800+
#
1801+
def fetch_values: (*int indexes) -> self
1802+
17661803
# <!--
17671804
# rdoc-file=array.c
17681805
# - fill(object, start = nil, count = nil) -> new_array

core/exception.rbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ class Exception
306306
#
307307
# See [Backtraces](rdoc-ref:exceptions.md@Backtraces).
308308
#
309-
def set_backtrace: (String | Array[String] backtrace) -> Array[String]
309+
def set_backtrace: (String | Array[String]) -> Array[String]
310+
| (Array[Thread::Backtrace::Location]) -> Array[Thread::Backtrace::Location]
310311
| (nil) -> nil
311312

312313
# <!--

core/fiber.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ class Fiber < Object
415415
# See Kernel#raise for more information.
416416
#
417417
def raise: (?string msg) -> untyped
418-
| (_Exception, ?string msg, ?Array[string] backtrace) -> untyped
418+
| (_Exception, ?string msg, ?Array[string] | Array[Thread::Backtrace::Location] | nil backtrace) -> untyped
419419

420420
# <!--
421421
# rdoc-file=cont.c

core/gc.rbs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,61 @@ module GC
157157
#
158158
OPTS: Array[String]
159159

160+
# <!--
161+
# rdoc-file=gc.rb
162+
# - GC.config -> hash
163+
# - GC.config(hash) -> hash
164+
# -->
165+
# Sets or gets information about the current GC config.
166+
#
167+
# Configuration parameters are GC implementation-specific and may change without
168+
# notice.
169+
#
170+
# This method can be called without parameters to retrieve the current config.
171+
#
172+
# This method can also be called with a `Hash` argument to assign values to
173+
# valid config keys. Config keys missing from the passed `Hash` will be left
174+
# unmodified.
175+
#
176+
# If a key/value pair is passed to this function that does not correspond to a
177+
# valid config key for the GC implementation being used, no config will be
178+
# updated, the key will be present in the returned Hash, and its value will be
179+
# `nil`. This is to facilitate easy migration between GC implementations.
180+
#
181+
# In both call-seqs, the return value of `GC.config` will be a `Hash` containing
182+
# the most recent full configuration, i.e., all keys and values defined by the
183+
# specific GC implementation being used. In the case of a config update, the
184+
# return value will include the new values being updated.
185+
#
186+
# This method is only expected to work on CRuby.
187+
#
188+
# Valid config keys for Ruby's default GC implementation are:
189+
#
190+
# rgengc_allow_full_mark
191+
# : Controls whether the GC is allowed to run a full mark (young & old
192+
# objects).
193+
#
194+
# When `true`, GC interleaves major and minor collections. This is the
195+
# default. GC will function as intended.
196+
#
197+
# When `false`, the GC will never trigger a full marking cycle unless
198+
# explicitly requested by user code. Instead, only a minor mark will
199+
# run—only young objects will be marked. When the heap space is exhausted,
200+
# new pages will be allocated immediately instead of running a full mark.
201+
#
202+
# A flag will be set to notify that a full mark has been requested. This
203+
# flag is accessible using `GC.latest_gc_info(:needs_major_by)`
204+
#
205+
# The user can trigger a major collection at any time using
206+
# `GC.start(full_mark: true)`
207+
#
208+
# When `false`, Young to Old object promotion is disabled. For performance
209+
# reasons, it is recommended to warm up an application using
210+
# `Process.warmup` before setting this parameter to `false`.
211+
#
212+
def self.config: () -> Hash[Symbol, untyped]
213+
| (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
214+
160215
# <!--
161216
# rdoc-file=gc.rb
162217
# - GC.count -> Integer

core/hash.rbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,9 +1844,9 @@ class Hash[unchecked out K, unchecked out V] < Object
18441844
# allocated with enough capacity to accommodate this many keys without having to
18451845
# be resized.
18461846
#
1847-
def initialize: () -> void
1848-
| (untyped default) -> void
1849-
| [A, B] () { (Hash[A, B] hash, A key) -> B } -> void
1847+
def initialize: (?capacity: int) -> void
1848+
| [V] (V default, ?capacity: int) -> void
1849+
| [A, B] (?capacity: int) { (Hash[A, B] hash, A key) -> B } -> void
18501850

18511851
# <!--
18521852
# rdoc-file=hash.c

core/kernel.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ module Kernel : BasicObject
983983
#
984984
def self?.fail: () -> bot
985985
| (string message, ?cause: Exception?) -> bot
986-
| (_Exception exception, ?_ToS? message, ?String | Array[String] | nil backtrace, ?cause: Exception?) -> bot
986+
| (_Exception exception, ?_ToS? message, ?String | Array[String] | Array[Thread::Backtrace::Location] | nil backtrace, ?cause: Exception?) -> bot
987987
| (_Exception exception, ?cause: Exception?, **untyped) -> bot
988988

989989
# <!--

core/match_data.rbs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,82 @@ class MatchData
132132
#
133133
def begin: (capture backref) -> Integer?
134134

135+
# <!--
136+
# rdoc-file=re.c
137+
# - bytebegin(n) -> integer
138+
# - bytebegin(name) -> integer
139+
# -->
140+
# Returns the offset (in bytes) of the beginning of the specified match.
141+
#
142+
# When non-negative integer argument `n` is given, returns the offset of the
143+
# beginning of the `n`th match:
144+
#
145+
# m = /(.)(.)(\d+)(\d)/.match("THX1138.")
146+
# # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
147+
# m[0] # => "HX1138"
148+
# m.bytebegin(0) # => 1
149+
# m[3] # => "113"
150+
# m.bytebegin(3) # => 3
151+
#
152+
# m = /(т)(е)(с)/.match('тест')
153+
# # => #<MatchData "тес" 1:"т" 2:"е" 3:"с">
154+
# m[0] # => "тес"
155+
# m.bytebegin(0) # => 0
156+
# m[3] # => "с"
157+
# m.bytebegin(3) # => 4
158+
#
159+
# When string or symbol argument `name` is given, returns the offset of the
160+
# beginning for the named match:
161+
#
162+
# m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
163+
# # => #<MatchData "hog" foo:"h" bar:"g">
164+
# m[:foo] # => "h"
165+
# m.bytebegin('foo') # => 0
166+
# m[:bar] # => "g"
167+
# m.bytebegin(:bar) # => 2
168+
#
169+
# Related: MatchData#byteend, MatchData#byteoffset.
170+
#
171+
def bytebegin: (capture backref) -> Integer?
172+
173+
# <!--
174+
# rdoc-file=re.c
175+
# - byteend(n) -> integer
176+
# - byteend(name) -> integer
177+
# -->
178+
# Returns the offset (in bytes) of the end of the specified match.
179+
#
180+
# When non-negative integer argument `n` is given, returns the offset of the end
181+
# of the `n`th match:
182+
#
183+
# m = /(.)(.)(\d+)(\d)/.match("THX1138.")
184+
# # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
185+
# m[0] # => "HX1138"
186+
# m.byteend(0) # => 7
187+
# m[3] # => "113"
188+
# m.byteend(3) # => 6
189+
#
190+
# m = /(т)(е)(с)/.match('тест')
191+
# # => #<MatchData "тес" 1:"т" 2:"е" 3:"с">
192+
# m[0] # => "тес"
193+
# m.byteend(0) # => 6
194+
# m[3] # => "с"
195+
# m.byteend(3) # => 6
196+
#
197+
# When string or symbol argument `name` is given, returns the offset of the end
198+
# for the named match:
199+
#
200+
# m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
201+
# # => #<MatchData "hog" foo:"h" bar:"g">
202+
# m[:foo] # => "h"
203+
# m.byteend('foo') # => 1
204+
# m[:bar] # => "g"
205+
# m.byteend(:bar) # => 3
206+
#
207+
# Related: MatchData#bytebegin, MatchData#byteoffset.
208+
#
209+
def byteend: (capture backref) -> Integer?
210+
135211
# <!--
136212
# rdoc-file=re.c
137213
# - mtch.byteoffset(n) -> array

core/ractor.rbs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,30 @@
249249
# See [Ractor design doc](rdoc-ref:ractor.md) for more details.
250250
#
251251
class Ractor
252+
# <!--
253+
# rdoc-file=ractor.rb
254+
# - _require(feature)
255+
# -->
256+
# internal method
257+
#
258+
def self._require: (String feature) -> bool
259+
260+
# <!--
261+
# rdoc-file=ractor.rb
262+
# - [](sym)
263+
# -->
264+
# get a value from ractor-local storage of current Ractor
265+
#
266+
def self.[]: (Symbol) -> untyped
267+
268+
# <!--
269+
# rdoc-file=ractor.rb
270+
# - []=(sym, val)
271+
# -->
272+
# set a value in ractor-local storage of current Ractor
273+
#
274+
def self.[]=: (Symbol, untyped) -> untyped
275+
252276
# <!--
253277
# rdoc-file=ractor.rb
254278
# - count()
@@ -280,7 +304,15 @@ class Ractor
280304
# -->
281305
# returns main ractor
282306
#
283-
def self.main: () -> untyped
307+
def self.main: () -> Ractor
308+
309+
# <!--
310+
# rdoc-file=ractor.rb
311+
# - main?()
312+
# -->
313+
# return true if the current ractor is main ractor
314+
#
315+
def self.main?: () -> boolish
284316

285317
# <!--
286318
# rdoc-file=ractor.rb

core/ruby_vm.rbs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,15 @@ module RubyVM::AbstractSyntaxTree
535535
#
536536
def last_column: () -> Integer
537537

538+
# <!--
539+
# rdoc-file=ast.rb
540+
# - node.locations -> array
541+
# -->
542+
# Returns location objects associated with the AST node. The returned array
543+
# contains RubyVM::AbstractSyntaxTree::Location.
544+
#
545+
def locations: () -> Array[Location]
546+
538547
# <!--
539548
# rdoc-file=ast.rb
540549
# - node.tokens -> array
@@ -579,6 +588,54 @@ module RubyVM::AbstractSyntaxTree
579588
#
580589
def children: () -> Array[untyped]
581590
end
591+
592+
# <!-- rdoc-file=ast.rb -->
593+
# RubyVM::AbstractSyntaxTree::Location instances are created by
594+
# RubyVM::AbstractSyntaxTree#locations.
595+
#
596+
# This class is MRI specific.
597+
#
598+
class Location
599+
# <!--
600+
# rdoc-file=ast.rb
601+
# - location.first_column -> integer
602+
# -->
603+
# The column number in the source code where this AST's text began.
604+
#
605+
def first_column: () -> Integer
606+
607+
# <!--
608+
# rdoc-file=ast.rb
609+
# - location.first_lineno -> integer
610+
# -->
611+
# The line number in the source code where this AST's text began.
612+
#
613+
def first_lineno: () -> Integer
614+
615+
# <!--
616+
# rdoc-file=ast.rb
617+
# - location.inspect -> string
618+
# -->
619+
# Returns debugging information about this location as a string.
620+
#
621+
def inspect: () -> String
622+
623+
# <!--
624+
# rdoc-file=ast.rb
625+
# - location.last_lineno -> integer
626+
# -->
627+
# The line number in the source code where this AST's text ended.
628+
#
629+
def last_lineno: () -> Integer
630+
631+
# <!--
632+
# rdoc-file=ast.rb
633+
# - location.last_column -> integer
634+
# -->
635+
# The column number in the source code where this AST's text ended.
636+
#
637+
def last_column: () -> Integer
638+
end
582639
end
583640

584641
# <!-- rdoc-file=yjit.rb -->

core/string.rbs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,30 @@ class String
939939
| [T < _ToStr] (Regexp regexp, MatchData::capture backref, T replacement) -> T
940940
| [T < _ToStr] (String substring, T replacement) -> T
941941

942+
# <!--
943+
# rdoc-file=string.c
944+
# - append_as_bytes(*objects) -> string
945+
# -->
946+
# Concatenates each object in `objects` into `self` without any encoding
947+
# validation or conversion and returns `self`:
948+
#
949+
# s = 'foo'
950+
# s.append_as_bytes(" \xE2\x82") # => "foo \xE2\x82"
951+
# s.valid_encoding? # => false
952+
# s.append_as_bytes("\xAC 12")
953+
# s.valid_encoding? # => true
954+
#
955+
# For each given object `object` that is an Integer, the value is considered a
956+
# Byte. If the Integer is bigger than one byte, only the lower byte is
957+
# considered, similar to String#setbyte:
958+
#
959+
# s = ""
960+
# s.append_as_bytes(0, 257) # => "\u0000\u0001"
961+
#
962+
# Related: String#<<, String#concat, which do an encoding aware concatenation.
963+
#
964+
def append_as_bytes: (String) -> String
965+
942966
# <!--
943967
# rdoc-file=string.c
944968
# - ascii_only? -> true or false

0 commit comments

Comments
 (0)