From d86c4d99b435c5f09f3e8180b5017a056b1bc207 Mon Sep 17 00:00:00 2001 From: SamW Date: Fri, 22 May 2026 17:02:53 -0700 Subject: [PATCH 01/15] update singleton methods --- core/array.rbs | 16 +++++++----- test/stdlib/Array_test.rb | 55 ++++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/core/array.rbs b/core/array.rbs index 188e0df81..68d2575d7 100644 --- a/core/array.rbs +++ b/core/array.rbs @@ -546,7 +546,7 @@ # given block. # %a{annotate:rdoc:source:from=array.c} -class Array[unchecked out E] < Object +class Array[unchecked out E] include Enumerable[E] # # Appends each argument in `objects` to `self`; returns `self`: @@ -2615,7 +2617,9 @@ class Array[unchecked out E] # # Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying). # - alias none? all? + def none?: () -> bool + | (_Pattern[E] pattern) -> bool + | () { (E element) -> boolish } -> bool # # Returns the count of elements in `self`: @@ -4140,6 +4147,7 @@ class Array[unchecked out E] alias initialize_copy replace end +%a{deprecated: Use Array::_Rand} interface _Rand def rand: (::Integer max) -> ::Integer end diff --git a/test/stdlib/Array_test.rb b/test/stdlib/Array_test.rb index 34659b84a..2dc426bfa 100644 --- a/test/stdlib/Array_test.rb +++ b/test/stdlib/Array_test.rb @@ -56,6 +56,10 @@ class ArrayInstanceTest < Test::Unit::TestCase class ArraySubclass < Array end + class RngGen + def rand(max) = ToInt.new(Random.new.rand(max)) + end + def test_op_and with_array [1r, 1i] do |other| assert_send_type '(array[untyped]) -> Array[Rational]', @@ -334,7 +338,21 @@ def test_fetch end def test_fetch_values - omit 'todo' + assert_send_type '() -> Array[Rational]', + [1r, 2r], :fetch_values + assert_send_type '() { (Integer) -> void } -> Array[Rational]', + [1r, 2r], :fetch_values do end + + with_int 1 do |index1| + with_int 2 do |index2| + assert_send_type '(*int) -> Array[Rational]', + [1r, 2r, 3r], :fetch_values, index1, index2 + assert_send_type '[I < _ToInt] (*I) { (I) -> void } -> Array[Rational]', + [1r, 2r, 3r], :fetch_values, index1, index2 do fail end + assert_send_type '[I < _ToInt] (*I) { (I) -> Complex } -> Array[Rational | Complex]', + [1r, 2r], :fetch_values, index1, index2 do |x| x.to_int.i end + end + end end def test_fill @@ -358,7 +376,17 @@ def test_index end def test_first - omit 'todo' + assert_send_type '() -> nil', + [], :first + assert_send_type '() -> Rational', + [1r, 2r], :first + + with_int 2 do |count| + assert_send_type '(int) -> Array[Rational]', + [], :first, count + assert_send_type '(int) -> Array[Rational]', + [1r, 2r], :first, count + end end def test_flatten @@ -412,7 +440,17 @@ def test_keep_if end def test_last - omit 'todo' + assert_send_type '() -> nil', + [], :last + assert_send_type '() -> Rational', + [1r, 2r], :last + + with_int 2 do |count| + assert_send_type '(int) -> Array[Rational]', + [], :last, count + assert_send_type '(int) -> Array[Rational]', + [1r, 2r], :last, count + end end def test_length(method: :length) @@ -500,7 +538,22 @@ def test_rotate! end def test_sample - omit 'todo' + assert_send_type '() -> nil', + [], :sample + assert_send_type '(random: Array::_Rand) -> nil', + [], :sample, random: RngGen.new + + assert_send_type '() -> Rational', + [1r, 2r, 3r], :sample + assert_send_type '(random: Array::_Rand) -> Rational', + [1r, 2r, 3r], :sample, random: RngGen.new + + with_int 2 do |count| + assert_send_type '(int) -> Array[Rational]', + [1r, 2r, 3r], :sample, count + assert_send_type '(int, random: Array::_Rand) -> Array[Rational]', + [1r, 2r, 3r], :sample, count, random: RngGen.new + end end def test_select(method: :select) @@ -524,11 +577,19 @@ def test_shift end def test_shuffle - omit 'todo' + assert_send_type '() -> Array[Rational]', + [1r, 2r, 3r], :shuffle + + assert_send_type '(random: Array::_Rand) -> Array[Rational]', + [1r, 2r, 3r], :shuffle, random: RngGen.new end def test_shuffle! - omit 'todo' + assert_send_type '() -> Array[Rational]', + [1r, 2r, 3r], :shuffle! + + assert_send_type '(random: Array::_Rand) -> Array[Rational]', + [1r, 2r, 3r], :shuffle!, random: RngGen.new end def test_slice! From 6e2f753704a2efa91c46c53e7eac323273e1439d Mon Sep 17 00:00:00 2001 From: SamW Date: Tue, 2 Jun 2026 11:20:45 -0700 Subject: [PATCH 07/15] more updates --- core/array.rbs | 20 +++---- test/stdlib/Array_test.rb | 118 ++++++++++++++++++++++++++++++++------ 2 files changed, 110 insertions(+), 28 deletions(-) diff --git a/core/array.rbs b/core/array.rbs index 943f2d7c2..80d8b7507 100644 --- a/core/array.rbs +++ b/core/array.rbs @@ -1397,7 +1397,7 @@ class Array[unchecked out E] # # Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning). # - def concat: (*::Array[E] arrays) -> self + def concat: (*array[E] other_arrays) -> self # # Prepends the given `objects` to `self`: @@ -2838,7 +2838,7 @@ class Array[unchecked out E] # # Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning). # - def push: (*E obj) -> self + def push: (*E objects) -> self # # Returns the new string formed by calling method #inspect on each @@ -3808,7 +3808,7 @@ class Array[unchecked out E] # # Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting). # - def transpose: () -> ::Array[::Array[untyped]] + def transpose: () -> Array[Array[untyped]] # # Returns the zero-based integer index of a specified element, or `nil`. @@ -2371,7 +2371,7 @@ class Array[unchecked out E] # # Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting). # - def join: (?string separator) -> String + def join: (?string? separator) -> String # TODO: where E: _ToS # # With a block given, calls the block with each element of `self`; returns a new @@ -2286,7 +2285,7 @@ class Array[unchecked out E] # # Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning). # - def insert: (int index, *E obj) -> self + def insert: (int index, *E objects) -> self # + # Returns the first element for which the block returns a truthy value. + # + # With a block given, calls the block with successive elements of the array; + # returns the first element for which the block returns a truthy value: + # + # [1, 3, 5].find {|element| element > 2} # => 3 + # + # If no such element is found, calls `if_none_proc` and returns its return + # value. + # + # [1, 3, 5].find(proc {-1}) {|element| element > 12} # => -1 + # + # With no block given, returns an Enumerator. + # + alias detect find # # With a block given, calls the block with each element of `self`; removes from @@ -2032,8 +2047,7 @@ class Array[unchecked out E] # # Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting). # - def filter!: () { (E item) -> boolish } -> self? - | () -> ::Enumerator[E, self?] + alias filter! select! # + # Freezes `self` (if not already frozen); returns `self`: + # + # a = [] + # a.frozen? # => false + # a.freeze + # a.frozen? # => true + # + # No further changes may be made to `self`; raises FrozenError if a change is + # attempted. + # + # Related: Kernel#frozen?. + # + def freeze: () -> self + # # With a block given, calls the block with each element of `self`; returns a new @@ -2713,8 +2745,8 @@ class Array[unchecked out E] # # Related: [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating). # - def permutation: (?int n) -> ::Enumerator[::Array[E], ::Array[E]] - | (?int n) { (::Array[E] p) -> void } -> ::Array[E] + def permutation: (?int? count) { (Array[E] permutation) -> void } -> self + | (?int? count) -> Enumerator[Array[E], self] # # Replaces the elements of `self` with the elements of `other_array`, which must @@ -3039,8 +3071,8 @@ class Array[unchecked out E] # # Related: see [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating). # - def reverse_each: () { (E item) -> void } -> self - | () -> ::Enumerator[E, self] + def reverse_each: () { (E element) -> void } -> self + | () -> Enumerator[E, self] #