Skip to content

Commit 8c80c3d

Browse files
authored
Merge pull request #2007 from ruby/rand
Make `rand(Integer)` returns `Integer`
2 parents 6539eb4 + c04c964 commit 8c80c3d

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Rubycw/Rubycw:
1111
Enabled: true
1212
Exclude:
1313
- 'test/**/*_test.rb'
14+
- 'test/typecheck/**/*.rb'
1415

1516
RBS:
1617
Enabled: true

core/kernel.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ module Kernel : BasicObject
13191319
# See also Random.rand.
13201320
#
13211321
def self?.rand: (?0) -> Float
1322-
| (int arg0) -> (Integer | Float)
1322+
| (int arg0) -> Integer
13231323
| (::Range[Integer] arg0) -> Integer?
13241324
| (::Range[Float] arg0) -> Float?
13251325

core/random.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Random < RBS::Unnamed::Random_Base
9191
#
9292
# See also Random#rand.
9393
#
94-
def self.rand: () -> Float
94+
def self.rand: (?0) -> Float
9595
| (Integer | ::Range[Integer] max) -> Integer
9696
| (Float | ::Range[Float] max) -> Float
9797
| [T < Numeric] (::Range[T]) -> T

core/rbs/unnamed/random.rbs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module RBS
4747
# (`-`) and add (`+`)methods, or rand will raise an ArgumentError.
4848
#
4949
%a{annotate:rdoc:copy:Random#rand}
50-
def rand: () -> Float
50+
def rand: (?0) -> Float
5151
| (Integer | ::Range[Integer] max) -> Integer
5252
| (Float | ::Range[Float] max) -> Float
5353

@@ -200,13 +200,13 @@ module RBS
200200
# Generates formatted random number from raw random bytes. See Random#rand.
201201
#
202202
%a{annotate:rdoc:copy:Random::Formatter#rand}
203-
def rand: () -> Float
204-
| (?Float? n) -> Float
205-
| (?Integer? n) -> Integer
206-
| (?Numeric? n) -> Numeric
207-
| (?::Range[Float]? n) -> Float
208-
| (?::Range[Integer]? n) -> Integer
209-
| (?::Range[Numeric]? n) -> Numeric
203+
def rand: (?0) -> Float
204+
| (Float? n) -> Float
205+
| (Integer n) -> Integer
206+
| (Numeric n) -> Numeric
207+
| (::Range[Float] n) -> Float
208+
| (::Range[Integer] n) -> Integer
209+
| (::Range[Numeric] n) -> Numeric
210210

211211
%a{annotate:rdoc:copy:Random::Formatter#random_byte}
212212
def random_bytes: (?Integer? n) -> String

test/typecheck/random/Steepfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
D = Steep::Diagnostic
2+
3+
target :test do
4+
signature "."
5+
check "."
6+
configure_code_diagnostics(D::Ruby.all_error)
7+
end

test/typecheck/random/test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# @type var int: Integer
2+
# @type var float: Float
3+
4+
float = rand(0)
5+
int = rand(1)
6+
7+
i = 2
8+
int = rand(i)
9+
10+
int = rand(1..10) || 0
11+
float = rand((1.0)..(2.2)) || 0.1
12+
13+
int = rand(1.1)

0 commit comments

Comments
 (0)