From 7b9724600826d0568ba6d020a31f87a5ec1fb12d Mon Sep 17 00:00:00 2001 From: SamW Date: Thu, 5 Mar 2026 14:40:07 -0800 Subject: [PATCH] update rbs signatures to be more consistent with generics --- core/array.rbs | 290 ++++++++++++++--------------- core/enumerable.rbs | 222 +++++++++++----------- core/enumerator.rbs | 50 ++--- core/enumerator/product.rbs | 10 +- core/object_space/weak_key_map.rbs | 14 +- core/range.rbs | 46 ++--- core/set.rbs | 6 +- core/struct.rbs | 32 ++-- core/thread.rbs | 12 +- docs/rbs_by_example.md | 40 ++-- docs/syntax.md | 4 +- sig/shims/enumerable.rbs | 6 +- stdlib/abbrev/0/array.rbs | 2 +- stdlib/csv/0/csv.rbs | 10 +- stdlib/json/0/json.rbs | 12 +- stdlib/shellwords/0/shellwords.rbs | 2 +- stdlib/tsort/0/cyclic.rbs | 2 +- stdlib/tsort/0/interfaces.rbs | 16 +- stdlib/tsort/0/tsort.rbs | 18 +- 19 files changed, 397 insertions(+), 397 deletions(-) diff --git a/core/array.rbs b/core/array.rbs index 887c1a6c9..e1c463e9d 100644 --- a/core/array.rbs +++ b/core/array.rbs @@ -545,8 +545,8 @@ # * #sum: Returns a sum of elements according to either `+` or a given block. # %a{annotate:rdoc:source:from=array.c} -class Array[unchecked out Elem] < Object - include Enumerable[Elem] +class Array[unchecked out E] < Object + include Enumerable[E] # # With a block given, calls the block with each element of `self`; returns a new @@ -1998,8 +1998,8 @@ class Array[unchecked out Elem] < Object # # Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching). # - def filter: () { (Elem item) -> boolish } -> ::Array[Elem] - | () -> ::Enumerator[Elem, ::Array[Elem]] + def filter: () { (E item) -> boolish } -> ::Array[E] + | () -> ::Enumerator[E, ::Array[E]] # # With a block given, calls the block with each element of `self`; removes from @@ -2016,8 +2016,8 @@ class Array[unchecked out Elem] < Object # # Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting). # - def filter!: () { (Elem item) -> boolish } -> self? - | () -> ::Enumerator[Elem, self?] + def filter!: () { (E item) -> boolish } -> self? + | () -> ::Enumerator[E, self?] # # Returns the zero-based integer index of a specified element, or `nil`. @@ -2267,7 +2267,7 @@ class Array[unchecked out Elem] < Object # # Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning). # - def insert: (int index, *Elem obj) -> self + def insert: (int index, *E obj) -> self # # Prepends the given `objects` to `self`: @@ -2786,10 +2786,10 @@ class Array[unchecked out Elem] < Object # # Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining). # - def product: () -> ::Array[[ Elem ]] - | [X] (::Array[X] other_ary) -> ::Array[[ Elem, X ]] - | [X, Y] (::Array[X] other_ary1, ::Array[Y] other_ary2) -> ::Array[[ Elem, X, Y ]] - | [U] (*::Array[U] other_arys) -> ::Array[::Array[Elem | U]] + def product: () -> ::Array[[ E ]] + | [X] (::Array[X] other_ary) -> ::Array[[ E, X ]] + | [X, Y] (::Array[X] other_ary1, ::Array[Y] other_ary2) -> ::Array[[ E, X, Y ]] + | [U] (*::Array[U] other_arys) -> ::Array[::Array[E | U]] # # Replaces the elements of `self` with the elements of `other_array`, which must @@ -2959,7 +2959,7 @@ class Array[unchecked out Elem] < Object # # Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning). # - def replace: (::Array[Elem]) -> self + def replace: (::Array[E]) -> self # # Returns the new string formed by calling method `#inspect` on each array @@ -3794,7 +3794,7 @@ class Array[unchecked out Elem] < Object # # Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining). # - def union: [T] (*::Array[T] other_arys) -> ::Array[T | Elem] + def union: [T] (*::Array[T] other_arys) -> ::Array[T | E] # # Returns an array of flattened objects returned by the block. @@ -395,8 +395,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem] # # Alias: #collect_concat. # - def collect_concat: [U] () { (Elem) -> (::Array[U] | U) } -> ::Array[U] - | () -> ::Enumerator[Elem, ::Array[untyped]] + def collect_concat: [U] () { (E) -> (::Array[U] | U) } -> ::Array[U] + | () -> ::Enumerator[E, ::Array[untyped]] # # Returns the first element for which the block returns a truthy value. @@ -484,8 +484,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem] # # With no block given, returns an Enumerator. # - def detect: (?Proc ifnone) { (Elem) -> boolish } -> Elem? - | (?Proc ifnone) -> ::Enumerator[Elem, Elem?] + def detect: (?Proc ifnone) { (E) -> boolish } -> E? + | (?Proc ifnone) -> ::Enumerator[E, E?] # # Returns an array containing the items in `self`: # # (0..4).to_a # => [0, 1, 2, 3, 4] # - def entries: () -> ::Array[Elem] + def entries: () -> ::Array[E] def enum_for: (Symbol method, *untyped, **untyped) ?{ (?) -> Integer } -> Enumerator[untyped, untyped] - | () ?{ () -> Integer } -> Enumerator[Elem, self] + | () ?{ () -> Integer } -> Enumerator[E, self] %a{annotate:rdoc:skip} alias to_enum enum_for @@ -635,8 +635,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem] # # Related: #reject. # - def find_all: () { (Elem) -> boolish } -> ::Array[Elem] - | () -> ::Enumerator[Elem, ::Array[Elem]] + def find_all: () { (E) -> boolish } -> ::Array[E] + | () -> ::Enumerator[E, ::Array[E]] # # Returns an array containing elements selected by the block. @@ -693,8 +693,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem] # With no argument and no block given, returns an Enumerator. # def find_index: (untyped value) -> Integer? - | () { (Elem) -> boolish } -> Integer? - | () -> ::Enumerator[Elem, Integer?] + | () { (E) -> boolish } -> Integer? + | () -> ::Enumerator[E, Integer?] # # Returns whether for any element `object == element`: @@ -812,7 +812,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem] # {foo: 0, bar: 1, baz: 2}.include?('foo') # => false # {foo: 0, bar: 1, baz: 2}.include?(0) # => false # - def include?: (Elem arg0) -> bool + def include?: (E arg0) -> bool # # Returns an array of objects returned by the block. @@ -1640,8 +1640,8 @@ module Enumerable[unchecked out Elem] : _Each[Elem] # # With no block given, returns an Enumerator. # - def map: [U] () { (Elem arg0) -> U } -> ::Array[U] - | () -> ::Enumerator[Elem, ::Array[untyped]] + def map: [U] () { (E arg0) -> U } -> ::Array[U] + | () -> ::Enumerator[E, ::Array[untyped]] # # Returns the result of applying a reducer to an initial value and the first @@ -1816,7 +1816,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem] # # (0..4).to_a # => [0, 1, 2, 3, 4] # - def to_a: () -> ::Array[Elem] + def to_a: () -> ::Array[E] # # Iterates the given block for each element with an arbitrary object, `obj`, and @@ -540,17 +540,17 @@ class Enumerator[unchecked out Elem, out Return = void] < Object # # => foo: 1 # # => foo: 2 # - def with_object: [U] (U obj) { (Elem, U obj) -> untyped } -> U - | [U] (U obj) -> ::Enumerator[[ Elem, U ], U] + def with_object: [U] (U obj) { (E, U obj) -> untyped } -> U + | [U] (U obj) -> ::Enumerator[[ E, U ], U] end # # Generator # -class Enumerator::Generator[out Elem] < Object - include Enumerable[Elem] +class Enumerator::Generator[out E] < Object + include Enumerable[E] - def each: () { (Elem) -> void } -> void + def each: () { (E) -> void } -> void end # @@ -619,7 +619,7 @@ end # # This returns an array of items like a normal enumerator does. # all_checked = active_items.select(&:checked) # -class Enumerator::Lazy[out Elem, out Return = void] < Enumerator[Elem, Return] +class Enumerator::Lazy[out E, out R = void] < Enumerator[E, R] # # Expands `lazy` enumerator to an array. See Enumerable#to_a. # @@ -631,7 +631,7 @@ class Enumerator::Lazy[out Elem, out Return = void] < Enumerator[Elem, Return] # --> # Like Enumerable#compact, but chains operation to be lazy-evaluated. # - def compact: () -> Enumerator::Lazy[Elem, Return] + def compact: () -> Enumerator::Lazy[E, R] # # Returns a non-lazy Enumerator converted from the lazy enumerator. # - def eager: () -> ::Enumerator[Elem, Return] + def eager: () -> ::Enumerator[E, R] end # @@ -674,7 +674,7 @@ end # # This type of objects can be created by Enumerable#chain and Enumerator#+. # -class Enumerator::Chain[out Elem] < Enumerator[Elem, void] +class Enumerator::Chain[out E] < Enumerator[E, void] # # Enumerator::Product generates a Cartesian product of any number of enumerable # objects. Iterating over the product of enumerable objects is roughly @@ -30,7 +30,7 @@ class Enumerator[unchecked out Elem, out Return = void] # # This type of objects can be created by Enumerator.product. # - class Product[unchecked out Elem] < Enumerator[Array[Elem], Product[Elem]] + class Product[unchecked out E] < Enumerator[Array[E], Product[E]] # # Returns `true` if `key` is a key in `self`, otherwise `false`. # - def key?: (Key) -> bool + def key?: (K) -> bool end end diff --git a/core/range.rbs b/core/range.rbs index 09838ed5f..000a3ee9f 100644 --- a/core/range.rbs +++ b/core/range.rbs @@ -233,8 +233,8 @@ # # require 'json/add/range' # -class Range[out Elem] < Object - include Enumerable[Elem] +class Range[out E] < Object + include Enumerable[E] # # Returns the values in `self` as an array: @@ -440,8 +440,8 @@ class Struct[Elem] # # Related: #each_pair. # - def each: () -> Enumerator[Elem, self] - | () { (Elem value) -> void } -> self + def each: () -> Enumerator[E, self] + | () { (E value) -> void } -> self # # With a block given, returns an array of values from `self` for which the block @@ -590,7 +590,7 @@ class Struct[Elem] # Raises RangeError if any element of the range is negative and out of range; # see Array@Array+Indexes. # - def values_at: (*int | range[int?] positions) -> Array[Elem] + def values_at: (*int | range[int?] positions) -> Array[E] # @@ -663,5 +663,5 @@ class Struct[Elem] # h = joe.deconstruct_keys(nil) # h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345} # - def deconstruct_keys: (Array[index & Hash::_Key]? indices) -> Hash[index & Hash::_Key, Elem] + def deconstruct_keys: (Array[index & Hash::_Key]? indices) -> Hash[index & Hash::_Key, E] end diff --git a/core/thread.rbs b/core/thread.rbs index 11d17a090..2d5004cfd 100644 --- a/core/thread.rbs +++ b/core/thread.rbs @@ -1598,7 +1598,7 @@ end # # consumer.join # -class Thread::Queue[Elem = untyped] < Object +class Thread::Queue[E = untyped] < Object # # Pushes the given `object` to the queue. # @@ -1713,7 +1713,7 @@ class Thread::Queue[Elem = untyped] < Object # If `timeout` seconds have passed and no data is available `nil` is returned. # If `timeout` is `0` it returns immediately. # - def pop: (?boolish non_block, ?timeout: _ToF?) -> Elem? + def pop: (?boolish non_block, ?timeout: _ToF?) -> E? # # Pushes the given `object` to the queue. # - def push: (Elem obj) -> void + def push: (E obj) -> void # # See #as_json. # - def self.json_create: [Elem] (Hash[String, String | Array[Elem]] object) -> Struct[Elem] + def self.json_create: [E] (Hash[String, String | Array[E]] object) -> Struct[E] # # Exception class to be raised when a cycle is found. # diff --git a/stdlib/tsort/0/interfaces.rbs b/stdlib/tsort/0/interfaces.rbs index d560681a4..8d410d939 100644 --- a/stdlib/tsort/0/interfaces.rbs +++ b/stdlib/tsort/0/interfaces.rbs @@ -1,20 +1,20 @@ %a{annotate:rdoc:skip} -module TSort[Node] - interface _Sortable[Node] +module TSort[N] + interface _Sortable[N] # #tsort_each_node is used to iterate for all nodes over a graph. # - def tsort_each_node: () { (Node) -> void } -> void + def tsort_each_node: () { (N) -> void } -> void # #tsort_each_child is used to iterate for child nodes of node. # - def tsort_each_child: (Node) { (Node) -> void } -> void + def tsort_each_child: (N) { (N) -> void } -> void end - interface _EachNode[Node] - def call: () { (Node) -> void } -> void + interface _EachNode[N] + def call: () { (N) -> void } -> void end - interface _EachChild[Node] - def call: (Node) { (Node) -> void } -> void + interface _EachChild[N] + def call: (N) { (N) -> void } -> void end end diff --git a/stdlib/tsort/0/tsort.rbs b/stdlib/tsort/0/tsort.rbs index 48946aba4..beaf4f37b 100644 --- a/stdlib/tsort/0/tsort.rbs +++ b/stdlib/tsort/0/tsort.rbs @@ -113,7 +113,7 @@ # 1. Tarjan, "Depth First Search and Linear Graph Algorithms", # *SIAM Journal on Computing*, Vol. 1, No. 2, pp. 146-160, June 1972. # -module TSort[Node] : TSort::_Sortable[Node] +module TSort[N] : TSort::_Sortable[N] #