Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions core/enumerator/arithmetic_sequence.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# <!-- rdoc-file=enumerator.c -->
# Enumerator::ArithmeticSequence is a subclass of Enumerator, that is a
# representation of sequences of numbers with common difference. Instances of
# this class can be generated by the Range#step and Numeric#step methods.
#
# The class can be used for slicing Array (see Array#slice) or custom
# collections.
#
class Enumerator::ArithmeticSequence < Enumerator[Numeric]
# <!--
# rdoc-file=enumerator.c
# - aseq.begin -> num or nil
# -->
# Returns the number that defines the first element of this arithmetic sequence.
#
def begin: () -> Numeric?

# <!--
# rdoc-file=enumerator.c
# - aseq.end -> num or nil
# -->
# Returns the number that defines the end of this arithmetic sequence.
#
def end: () -> Numeric?

# <!--
# rdoc-file=enumerator.c
# - aseq.each {|i| block } -> aseq
# - aseq.each -> aseq
# -->
#
def each: () ?{ (Numeric) -> void } -> self

# <!--
# rdoc-file=enumerator.c
# - aseq.exclude_end? -> true or false
# -->
# Returns `true` if this arithmetic sequence excludes its end value.
#
def exclude_end?: () -> bool

# <!--
# rdoc-file=enumerator.c
# - aseq.last -> num or nil
# - aseq.last(n) -> an_array
# -->
# Returns the last number in this arithmetic sequence, or an array of the last
# `n` elements.
#
def last: () -> Numeric?
| (Integer n) -> Array[Numeric]

# <!--
# rdoc-file=enumerator.c
# - aseq.size -> num or nil
# -->
# Returns the number of elements in this arithmetic sequence if it is a finite
# sequence. Otherwise, returns `nil`.
#
def size: () -> (Integer | Float)

# <!--
# rdoc-file=enumerator.c
# - aseq.step -> num
# -->
# Returns the number that defines the common difference between two adjacent
# elements in this arithmetic sequence.
#
def step: () -> Numeric
end
5 changes: 0 additions & 5 deletions core/float.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -872,11 +872,6 @@ class Float < Numeric
def round: (?half: :up | :down | :even) -> Integer
| (int digits, ?half: :up | :down | :even) -> (Integer | Float)

def step: (?Numeric limit, ?Numeric step) { (Float) -> void } -> self
| (?Numeric limit, ?Numeric step) -> Enumerator[Float, self]
| (?by: Numeric, ?to: Numeric) { (Float) -> void } -> self
| (?by: Numeric, ?to: Numeric) -> Enumerator[Float, self]

# <!--
# rdoc-file=numeric.rb
# - to_f -> self
Expand Down
11 changes: 0 additions & 11 deletions core/integer.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,17 +1155,6 @@ class Integer < Numeric
#
def size: () -> Integer

def step: () { (Integer) -> void } -> void
| (Numeric limit, ?Integer step) { (Integer) -> void } -> void
| (Numeric limit, ?Numeric step) { (Numeric) -> void } -> void
| (to: Numeric, ?by: Integer) { (Integer) -> void } -> void
| (by: Numeric, ?to: Numeric) { (Numeric) -> void } -> void
| () -> Enumerator[Integer, bot]
| (Numeric limit, ?Integer step) -> Enumerator[Integer]
| (Numeric limit, ?Numeric step) -> Enumerator[Numeric]
| (to: Numeric, ?by: Integer) -> Enumerator[Integer]
| (by: Numeric, ?to: Numeric) -> Enumerator[Numeric]

# <!--
# rdoc-file=numeric.c
# - succ -> next_integer
Expand Down
4 changes: 2 additions & 2 deletions core/numeric.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,9 @@ class Numeric
# where *n = (limit - self)/step*.
#
def step: (?Numeric limit, ?Numeric step) { (Numeric) -> void } -> self
| (?Numeric limit, ?Numeric step) -> Enumerator[Numeric, self]
| (?Numeric limit, ?Numeric step) -> Enumerator::ArithmeticSequence
| (?by: Numeric, ?to: Numeric) { (Numeric) -> void } -> self
| (?by: Numeric, ?to: Numeric) -> Enumerator[Numeric, self]
| (?by: Numeric, ?to: Numeric) -> Enumerator::ArithmeticSequence

# <!--
# rdoc-file=complex.c
Expand Down
5 changes: 0 additions & 5 deletions core/rational.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,6 @@ class Rational < Numeric
def round: (?half: :up | :down | :even) -> Integer
| (Integer digits, ?half: :up | :down | :even) -> (Integer | Rational)

def step: (?Numeric limit, ?Numeric step) { (Rational) -> void } -> self
| (?Numeric limit, ?Numeric step) -> Enumerator[Rational, self]
| (?by: Numeric, ?to: Numeric) { (Rational) -> void } -> self
| (?by: Numeric, ?to: Numeric) -> Enumerator[Rational, self]

# <!--
# rdoc-file=rational.c
# - rat.to_f -> float
Expand Down
27 changes: 27 additions & 0 deletions test/stdlib/Numeric_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,31 @@ def test_to_c
assert_send_type '() -> Complex', 1.0, :to_c
assert_send_type '() -> Complex', 1r, :to_c
end

def test_step
assert_send_type '() -> Enumerator::ArithmeticSequence',
1, :step
assert_send_type '() { (Integer) -> void } -> Integer',
1, :step do |i| break_from_block i end
assert_send_type '(Float) -> Enumerator::ArithmeticSequence',
1, :step, 1.5
assert_send_type '(Float) { (Float) -> void } -> Integer',
1, :step, 1.5 do |i| break_from_block i end
assert_send_type '(Integer, Float) -> Enumerator::ArithmeticSequence',
1r, :step, 2, 0.2
assert_send_type '(Integer, Float) { (Float) -> void } -> Rational',
1r, :step, 2, 0.2 do |i| end
assert_send_type '(to: Rational) -> Enumerator::ArithmeticSequence',
1, :step, to: 2r
assert_send_type '(to: Rational) { (Integer) -> void } -> Integer',
1, :step, to: 2r do |i| end
assert_send_type '(by: Float) -> Enumerator::ArithmeticSequence',
1, :step, by: 0.2
assert_send_type '(by: Float) { (Float) -> void } -> Rational',
1, :step, by: 0.2 do |i| break_from_block i end
assert_send_type '(to: Rational, by: Float) -> Enumerator::ArithmeticSequence',
1, :step, to: 3r, by: 0.2
assert_send_type '(to: Rational, by: Float) { (Float) -> void } -> Rational',
1, :step, to: 3r, by: 0.2 do |i| break_from_block i end
end
end
45 changes: 45 additions & 0 deletions test/stdlib/enumerator/ArithmeticSequence_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require_relative "../test_helper"

class EnumeratorArithmeticSequenceInstanceTest < Test::Unit::TestCase
include TestHelper

testing "::Enumerator::ArithmeticSequence"

def test_begin
assert_send_type "() -> Integer", 1.step(2), :begin
assert_send_type "() -> nil", (..3).step(1), :begin
end

def test_end
assert_send_type "() -> Integer", 1.step(2), :end
assert_send_type "() -> nil", (1..).step(1), :end
end

def test_each
assert_send_type "() -> Enumerator::ArithmeticSequence", 1.step(2), :each
assert_send_type "() { (Integer) -> void } -> Enumerator::ArithmeticSequence", 1.step(2), :each do |i| end
assert_send_type "() { (Float) -> void } -> Enumerator::ArithmeticSequence", 1.0.step(2), :each do |i| end
assert_send_type "() { (Float) -> void } -> Enumerator::ArithmeticSequence", 1.step(2.0), :each do |i| end
assert_send_type "() { (Float) -> void } -> Enumerator::ArithmeticSequence", 1.step(2, 1.0), :each do |i| end
end

def test_exclude_end?
assert_send_type "() -> bool", 1.step(2), :exclude_end?
end

def test_first
assert_send_type "() -> Integer", 1.step(2), :first
end

def test_last
assert_send_type "() -> Integer", 1.step(2), :last
end

def test_size
assert_send_type "() -> Integer", 1.step(2), :size
end

def test_step
assert_send_type "() -> Integer", 1.step(2), :step
end
end
Loading