Skip to content

Commit 70ee4e7

Browse files
committed
Refine signature of Numeric#step
And add type of `Enumerator::ArithmeticSequence`.
1 parent 03c0e25 commit 70ee4e7

File tree

7 files changed

+151
-23
lines changed

7 files changed

+151
-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
@@ -898,11 +898,6 @@ class Float < Numeric
898898
def round: (?half: :up | :down | :even) -> Integer
899899
| (int digits, ?half: :up | :down | :even) -> (Integer | Float)
900900

901-
def step: (?Numeric limit, ?Numeric step) { (Float) -> void } -> self
902-
| (?Numeric limit, ?Numeric step) -> Enumerator[Float, self]
903-
| (?by: Numeric, ?to: Numeric) { (Float) -> void } -> self
904-
| (?by: Numeric, ?to: Numeric) -> Enumerator[Float, self]
905-
906901
def to_c: () -> Complex
907902

908903
# <!--

core/integer.rbs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,17 +1187,6 @@ class Integer < Numeric
11871187
#
11881188
def size: () -> Integer
11891189

1190-
def step: () { (Integer) -> void } -> void
1191-
| (Numeric limit, ?Integer step) { (Integer) -> void } -> void
1192-
| (Numeric limit, ?Numeric step) { (Numeric) -> void } -> void
1193-
| (to: Numeric, ?by: Integer) { (Integer) -> void } -> void
1194-
| (by: Numeric, ?to: Numeric) { (Numeric) -> void } -> void
1195-
| () -> Enumerator[Integer, bot]
1196-
| (Numeric limit, ?Integer step) -> Enumerator[Integer]
1197-
| (Numeric limit, ?Numeric step) -> Enumerator[Numeric]
1198-
| (to: Numeric, ?by: Integer) -> Enumerator[Integer]
1199-
| (by: Numeric, ?to: Numeric) -> Enumerator[Numeric]
1200-
12011190
# <!--
12021191
# rdoc-file=numeric.c
12031192
# - 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
@@ -438,11 +438,6 @@ class Rational < Numeric
438438
def round: (?half: :up | :down | :even) -> Integer
439439
| (Integer digits, ?half: :up | :down | :even) -> (Integer | Rational)
440440

441-
def step: (?Numeric limit, ?Numeric step) { (Rational) -> void } -> self
442-
| (?Numeric limit, ?Numeric step) -> Enumerator[Rational, self]
443-
| (?by: Numeric, ?to: Numeric) { (Rational) -> void } -> self
444-
| (?by: Numeric, ?to: Numeric) -> Enumerator[Rational, self]
445-
446441
def to_c: () -> Complex
447442

448443
# <!--

test/stdlib/Numeric_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require_relative "test_helper"
2+
3+
class NumericInstanceTest < Test::Unit::TestCase
4+
include TestHelper
5+
6+
testing 'Numeric'
7+
8+
def test_step
9+
assert_send_type '() -> Enumerator::ArithmeticSequence',
10+
1, :step
11+
assert_send_type '() { (Integer) -> void } -> Integer',
12+
1, :step do |i| break_from_block i end
13+
assert_send_type '(Float) -> Enumerator::ArithmeticSequence',
14+
1, :step, 1.5
15+
assert_send_type '(Float) { (Float) -> void } -> Integer',
16+
1, :step, 1.5 do |i| break_from_block i end
17+
assert_send_type '(Integer, Float) -> Enumerator::ArithmeticSequence',
18+
1r, :step, 2, 0.2
19+
assert_send_type '(Integer, Float) { (Float) -> void } -> Rational',
20+
1r, :step, 2, 0.2 do |i| end
21+
assert_send_type '(to: Rational) -> Enumerator::ArithmeticSequence',
22+
1, :step, to: 2r
23+
assert_send_type '(to: Rational) { (Integer) -> void } -> Integer',
24+
1, :step, to: 2r do |i| end
25+
assert_send_type '(by: Float) -> Enumerator::ArithmeticSequence',
26+
1, :step, by: 0.2
27+
assert_send_type '(by: Float) { (Float) -> void } -> Rational',
28+
1, :step, by: 0.2 do |i| break_from_block i end
29+
assert_send_type '(to: Rational, by: Float) -> Enumerator::ArithmeticSequence',
30+
1, :step, to: 3r, by: 0.2
31+
assert_send_type '(to: Rational, by: Float) { (Float) -> void } -> Rational',
32+
1, :step, to: 3r, by: 0.2 do |i| break_from_block i end
33+
end
34+
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)