Skip to content

Commit

Permalink
Merge pull request #1958 from zverok/range-step-behavior-change
Browse files Browse the repository at this point in the history
Support Range#step behavior change in Ruby 3.4
  • Loading branch information
soutaro authored Dec 19, 2024
2 parents e80f75b + bc6b2b0 commit da70891
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/range.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,8 +1017,10 @@ class Range[out Elem] < Object
# ('a'..'e').step { p _1 }
# # Default step 1; prints: a, b, c, d, e
#
def step: (?Numeric | int n) -> Enumerator[Elem, self]
| (?Numeric | int n) { (Elem element) -> void } -> self
def step: (?Numeric | int) -> Enumerator[Elem, self]
| (?Numeric | int) { (Elem element) -> void } -> self
| (untyped) -> Enumerator[Elem, self]
| (untyped) { (Elem element) -> void } -> self

# <!--
# rdoc-file=range.c
Expand Down
5 changes: 5 additions & 0 deletions test/stdlib/Range_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ def test_size
def test_step
(1..10).step
(1..10).step(2)

if_ruby(..."3.4.0", skip: false) do
('A'...'Z').step { |s| s.downcase }
('A'...'Z').step(2) { |s| s.downcase }
end

if_ruby("3.4.0"..., skip: false) do
('A'...'AAA').step('A') { |s| s.downcase }
end
end

def test_to_s
Expand Down
7 changes: 7 additions & 0 deletions test/typecheck/range_step/Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
D = Steep::Diagnostic

target :test do
signature "."
check "."
configure_code_diagnostics(D::Ruby.all_error)
end
10 changes: 10 additions & 0 deletions test/typecheck/range_step/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# These are valid because step is a number.

(1...3).step(1) { }
("A".."C").step(1) { }

# This works because "A" + "" is valid.
("A".."C").step("") { }

# This doesn't work but the type checker cannot detect it.
("A".."C").step(Exception.new) { }

0 comments on commit da70891

Please sign in to comment.