diff --git a/core/array.rbs b/core/array.rbs index a110da352..739c693d6 100644 --- a/core/array.rbs +++ b/core/array.rbs @@ -2019,6 +2019,32 @@ class Array[unchecked out Elem] < Object def filter!: () { (Elem item) -> boolish } -> self? | () -> ::Enumerator[Elem, 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: + # + # (0..9).find {|element| element > 2} # => 3 + # + # If no such element is found, calls `if_none_proc` and returns its return + # value. + # + # (0..9).find(proc {false}) {|element| element > 12} # => false + # {foo: 0, bar: 1, baz: 2}.find {|key, value| key.start_with?('b') } # => [:bar, 1] + # {foo: 0, bar: 1, baz: 2}.find(proc {[]}) {|key, value| key.start_with?('c') } # => [] + # + # With no block given, returns an Enumerator. + # + def find: () { (Elem) -> boolish } -> Elem? + | () -> ::Enumerator[Elem, Elem?] + | [T] (Enumerable::_NotFound[T] ifnone) { (Elem) -> boolish } -> (Elem | T) + | [T] (Enumerable::_NotFound[T] ifnone) -> ::Enumerator[Elem, Elem | T] + # + # Returns the last element for which the block returns a truthy value. + # + # With a block given, calls the block with successive elements of the array in + # reverse order; returns the last element for which the block returns a truthy + # value: + # + # (0..9).rfind {|element| element < 5} # => 4 + # + # If no such element is found, calls `if_none_proc` and returns its return + # value. + # + # (0..9).rfind(proc {false}) {|element| element < -2} # => false + # {foo: 0, bar: 1, baz: 2}.rfind {|key, value| key.start_with?('b') } # => [:baz, 2] + # {foo: 0, bar: 1, baz: 2}.rfind(proc {[]}) {|key, value| key.start_with?('c') } # => [] + # + # With no block given, returns an Enumerator. + # + def rfind: () { (Elem) -> boolish } -> Elem? + | () -> ::Enumerator[Elem, Elem?] + | [T] (Enumerable::_NotFound[T] ifnone) { (Elem) -> boolish } -> (Elem | T) + | [T] (Enumerable::_NotFound[T] ifnone) -> ::Enumerator[Elem, Elem | T] + # +# The [Ruby](rdoc-ref:Ruby) module that contains portable information among +# implementations. +# +# The constants defined here are aliased in the toplevel with `RUBY_` prefix. +# +module Ruby + # + # The copyright string for ruby + # + COPYRIGHT: ::String + + # + # The full ruby version string, like `ruby -v` prints + # + DESCRIPTION: ::String + + # + # The engine or interpreter this ruby uses. + # + ENGINE: ::String + + # + # The version of the engine or interpreter this ruby uses. + # + ENGINE_VERSION: ::String + + # + # The patchlevel for this ruby. If this is a development build of ruby the + # patchlevel will be -1 + # + PATCHLEVEL: ::Integer + + # + # The platform for this ruby + # + PLATFORM: ::String + + # + # The date this ruby was released + # + RELEASE_DATE: ::String + + # + # The GIT commit hash for this ruby. + # + REVISION: ::String + + # + # The running version of ruby + # + VERSION: ::String +end diff --git a/test/stdlib/Array_test.rb b/test/stdlib/Array_test.rb index 4165b3e76..8c2410f2a 100644 --- a/test/stdlib/Array_test.rb +++ b/test/stdlib/Array_test.rb @@ -411,6 +411,20 @@ def test_filter! [1,2,3], :filter! end + def test_find + assert_send_type "() { (Integer) -> bool } -> Integer", + [1,2,3], :find, &->(i) { i.odd? } + assert_send_type "() { (Integer) -> bool } -> nil", + [0,2], :find, &->(i) { i.odd? } + assert_send_type "(Enumerable::_NotFound[String]) { (Integer) -> bool } -> String", + [0,2], :find, -> { "" }, &->(i) { i.odd? } + + assert_send_type "() -> Enumerator[Integer, Integer?]", + [1,2,3], :find + assert_send_type "(Enumerable::_NotFound[String]) -> Enumerator[Integer, Integer | String | nil]", + [0,2], :find, -> { "" } + end + def test_find_index assert_send_type "(Integer) -> Integer", [1,2,3], :find_index, 1 @@ -724,6 +738,20 @@ def test_reverse_each [2,3,4], :reverse_each end + def test_rfind + assert_send_type "() { (Integer) -> bool } -> Integer", + [1,2,3], :rfind, &->(i) { i.odd? } + assert_send_type "() { (Integer) -> bool } -> nil", + [0,2], :rfind, &->(i) { i.odd? } + assert_send_type "(Enumerable::_NotFound[String]) { (Integer) -> bool } -> String", + [0,2], :rfind, -> { "" }, &->(i) { i.odd? } + + assert_send_type "() -> Enumerator[Integer, Integer?]", + [1,2,3], :rfind + assert_send_type "(Enumerable::_NotFound[String]) -> Enumerator[Integer, Integer | String | nil]", + [0,2], :rfind, -> { "" } + end + def test_rindex assert_send_type "(Integer) -> Integer", [1,2,3], :rindex, 1 diff --git a/test/stdlib/Ruby_test.rb b/test/stdlib/Ruby_test.rb new file mode 100644 index 000000000..80e2ee76b --- /dev/null +++ b/test/stdlib/Ruby_test.rb @@ -0,0 +1,20 @@ +require_relative "test_helper" +require "resolv" + +class RubyTest < Test::Unit::TestCase + include TestHelper + + testing "::Ruby" + + def test_constants + assert_const_type '::String', 'Ruby::COPYRIGHT' + assert_const_type '::String', 'Ruby::DESCRIPTION' + assert_const_type '::String', 'Ruby::ENGINE' + assert_const_type '::String', 'Ruby::ENGINE_VERSION' + assert_const_type '::Integer', 'Ruby::PATCHLEVEL' + assert_const_type '::String', 'Ruby::PLATFORM' + assert_const_type '::String', 'Ruby::RELEASE_DATE' + assert_const_type '::String', 'Ruby::REVISION' + assert_const_type '::String', 'Ruby::VERSION' + end +end