Skip to content

Commit de08397

Browse files
authored
Merge pull request #2600 from ksss/numeric-step
Refine signature of `Numeric#step` and add type of `Enumerator::ArithmeticSequence`
2 parents 25b36b5 + 3f6d30f commit de08397

File tree

7 files changed

+144
-23
lines changed

7 files changed

+144
-23
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# <!-- rdoc-file=enumerator.c -->
2+
# Enumerator::ArithmeticSequence is a subclass of Enumerator, that is a
3+
# representation of sequences of numbers with common difference. Instances of
4+
# this class can be generated by the Range#step and Numeric#step methods.
5+
#
6+
# The class can be used for slicing Array (see Array#slice) or custom
7+
# collections.
8+
#
9+
class Enumerator::ArithmeticSequence < Enumerator[Numeric]
10+
# <!--
11+
# rdoc-file=enumerator.c
12+
# - aseq.begin -> num or nil
13+
# -->
14+
# Returns the number that defines the first element of this arithmetic sequence.
15+
#
16+
def begin: () -> Numeric?
17+
18+
# <!--
19+
# rdoc-file=enumerator.c
20+
# - aseq.end -> num or nil
21+
# -->
22+
# Returns the number that defines the end of this arithmetic sequence.
23+
#
24+
def end: () -> Numeric?
25+
26+
# <!--
27+
# rdoc-file=enumerator.c
28+
# - aseq.each {|i| block } -> aseq
29+
# - aseq.each -> aseq
30+
# -->
31+
#
32+
def each: () ?{ (Numeric) -> void } -> self
33+
34+
# <!--
35+
# rdoc-file=enumerator.c
36+
# - aseq.exclude_end? -> true or false
37+
# -->
38+
# Returns `true` if this arithmetic sequence excludes its end value.
39+
#
40+
def exclude_end?: () -> bool
41+
42+
# <!--
43+
# rdoc-file=enumerator.c
44+
# - aseq.last -> num or nil
45+
# - aseq.last(n) -> an_array
46+
# -->
47+
# Returns the last number in this arithmetic sequence, or an array of the last
48+
# `n` elements.
49+
#
50+
def last: () -> Numeric?
51+
| (Integer n) -> Array[Numeric]
52+
53+
# <!--
54+
# rdoc-file=enumerator.c
55+
# - aseq.size -> num or nil
56+
# -->
57+
# Returns the number of elements in this arithmetic sequence if it is a finite
58+
# sequence. Otherwise, returns `nil`.
59+
#
60+
def size: () -> (Integer | Float)
61+
62+
# <!--
63+
# rdoc-file=enumerator.c
64+
# - aseq.step -> num
65+
# -->
66+
# Returns the number that defines the common difference between two adjacent
67+
# elements in this arithmetic sequence.
68+
#
69+
def step: () -> Numeric
70+
end

core/float.rbs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -872,11 +872,6 @@ class Float < Numeric
872872
def round: (?half: :up | :down | :even) -> Integer
873873
| (int digits, ?half: :up | :down | :even) -> (Integer | Float)
874874

875-
def step: (?Numeric limit, ?Numeric step) { (Float) -> void } -> self
876-
| (?Numeric limit, ?Numeric step) -> Enumerator[Float, self]
877-
| (?by: Numeric, ?to: Numeric) { (Float) -> void } -> self
878-
| (?by: Numeric, ?to: Numeric) -> Enumerator[Float, self]
879-
880875
# <!--
881876
# rdoc-file=numeric.rb
882877
# - to_f -> self

core/integer.rbs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,17 +1155,6 @@ class Integer < Numeric
11551155
#
11561156
def size: () -> Integer
11571157

1158-
def step: () { (Integer) -> void } -> void
1159-
| (Numeric limit, ?Integer step) { (Integer) -> void } -> void
1160-
| (Numeric limit, ?Numeric step) { (Numeric) -> void } -> void
1161-
| (to: Numeric, ?by: Integer) { (Integer) -> void } -> void
1162-
| (by: Numeric, ?to: Numeric) { (Numeric) -> void } -> void
1163-
| () -> Enumerator[Integer, bot]
1164-
| (Numeric limit, ?Integer step) -> Enumerator[Integer]
1165-
| (Numeric limit, ?Numeric step) -> Enumerator[Numeric]
1166-
| (to: Numeric, ?by: Integer) -> Enumerator[Integer]
1167-
| (by: Numeric, ?to: Numeric) -> Enumerator[Numeric]
1168-
11691158
# <!--
11701159
# rdoc-file=numeric.c
11711160
# - succ -> next_integer

core/numeric.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,9 @@ class Numeric
749749
# where *n = (limit - self)/step*.
750750
#
751751
def step: (?Numeric limit, ?Numeric step) { (Numeric) -> void } -> self
752-
| (?Numeric limit, ?Numeric step) -> Enumerator[Numeric, self]
752+
| (?Numeric limit, ?Numeric step) -> Enumerator::ArithmeticSequence
753753
| (?by: Numeric, ?to: Numeric) { (Numeric) -> void } -> self
754-
| (?by: Numeric, ?to: Numeric) -> Enumerator[Numeric, self]
754+
| (?by: Numeric, ?to: Numeric) -> Enumerator::ArithmeticSequence
755755

756756
# <!--
757757
# rdoc-file=complex.c

core/rational.rbs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,6 @@ class Rational < Numeric
400400
def round: (?half: :up | :down | :even) -> Integer
401401
| (Integer digits, ?half: :up | :down | :even) -> (Integer | Rational)
402402

403-
def step: (?Numeric limit, ?Numeric step) { (Rational) -> void } -> self
404-
| (?Numeric limit, ?Numeric step) -> Enumerator[Rational, self]
405-
| (?by: Numeric, ?to: Numeric) { (Rational) -> void } -> self
406-
| (?by: Numeric, ?to: Numeric) -> Enumerator[Rational, self]
407-
408403
# <!--
409404
# rdoc-file=rational.c
410405
# - rat.to_f -> float

test/stdlib/Numeric_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,31 @@ def test_to_c
3939
assert_send_type '() -> Complex', 1.0, :to_c
4040
assert_send_type '() -> Complex', 1r, :to_c
4141
end
42+
43+
def test_step
44+
assert_send_type '() -> Enumerator::ArithmeticSequence',
45+
1, :step
46+
assert_send_type '() { (Integer) -> void } -> Integer',
47+
1, :step do |i| break_from_block i end
48+
assert_send_type '(Float) -> Enumerator::ArithmeticSequence',
49+
1, :step, 1.5
50+
assert_send_type '(Float) { (Float) -> void } -> Integer',
51+
1, :step, 1.5 do |i| break_from_block i end
52+
assert_send_type '(Integer, Float) -> Enumerator::ArithmeticSequence',
53+
1r, :step, 2, 0.2
54+
assert_send_type '(Integer, Float) { (Float) -> void } -> Rational',
55+
1r, :step, 2, 0.2 do |i| end
56+
assert_send_type '(to: Rational) -> Enumerator::ArithmeticSequence',
57+
1, :step, to: 2r
58+
assert_send_type '(to: Rational) { (Integer) -> void } -> Integer',
59+
1, :step, to: 2r do |i| end
60+
assert_send_type '(by: Float) -> Enumerator::ArithmeticSequence',
61+
1, :step, by: 0.2
62+
assert_send_type '(by: Float) { (Float) -> void } -> Rational',
63+
1, :step, by: 0.2 do |i| break_from_block i end
64+
assert_send_type '(to: Rational, by: Float) -> Enumerator::ArithmeticSequence',
65+
1, :step, to: 3r, by: 0.2
66+
assert_send_type '(to: Rational, by: Float) { (Float) -> void } -> Rational',
67+
1, :step, to: 3r, by: 0.2 do |i| break_from_block i end
68+
end
4269
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require_relative "../test_helper"
2+
3+
class EnumeratorArithmeticSequenceInstanceTest < Test::Unit::TestCase
4+
include TestHelper
5+
6+
testing "::Enumerator::ArithmeticSequence"
7+
8+
def test_begin
9+
assert_send_type "() -> Integer", 1.step(2), :begin
10+
assert_send_type "() -> nil", (..3).step(1), :begin
11+
end
12+
13+
def test_end
14+
assert_send_type "() -> Integer", 1.step(2), :end
15+
assert_send_type "() -> nil", (1..).step(1), :end
16+
end
17+
18+
def test_each
19+
assert_send_type "() -> Enumerator::ArithmeticSequence", 1.step(2), :each
20+
assert_send_type "() { (Integer) -> void } -> Enumerator::ArithmeticSequence", 1.step(2), :each do |i| end
21+
assert_send_type "() { (Float) -> void } -> Enumerator::ArithmeticSequence", 1.0.step(2), :each do |i| end
22+
assert_send_type "() { (Float) -> void } -> Enumerator::ArithmeticSequence", 1.step(2.0), :each do |i| end
23+
assert_send_type "() { (Float) -> void } -> Enumerator::ArithmeticSequence", 1.step(2, 1.0), :each do |i| end
24+
end
25+
26+
def test_exclude_end?
27+
assert_send_type "() -> bool", 1.step(2), :exclude_end?
28+
end
29+
30+
def test_first
31+
assert_send_type "() -> Integer", 1.step(2), :first
32+
end
33+
34+
def test_last
35+
assert_send_type "() -> Integer", 1.step(2), :last
36+
end
37+
38+
def test_size
39+
assert_send_type "() -> Integer", 1.step(2), :size
40+
end
41+
42+
def test_step
43+
assert_send_type "() -> Integer", 1.step(2), :step
44+
end
45+
end

0 commit comments

Comments
 (0)