1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

rand(beginless_range) raise Errno::EDOM instead of TypeError

same as `rand(endless_range)`

Before:
```
$ ruby -e 'rand(..1)'
Traceback (most recent call last):
	2: from -e:1:in `<main>'
	1: from -e:1:in `rand'
-e:1:in `-': nil can't be coerced into Integer (TypeError)
```

After:
```
$ ruby -e 'rand(..1)'
Traceback (most recent call last):
	1: from -e:1:in `<main>'
-e:1:in `rand': Numerical argument out of domain (Errno::EDOM)
```
This commit is contained in:
Kazuhiro NISHIYAMA 2019-12-15 14:47:36 +09:00
parent db2ea9b0c5
commit e2b192f7d5
No known key found for this signature in database
GPG key ID: 262ED8DBB4222F7A
2 changed files with 5 additions and 2 deletions

View file

@ -1043,9 +1043,11 @@ random_s_bytes(VALUE obj, VALUE len)
static VALUE
range_values(VALUE vmax, VALUE *begp, VALUE *endp, int *exclp)
{
VALUE end;
VALUE beg, end;
if (!rb_range_values(vmax, begp, &end, exclp)) return Qfalse;
if (!rb_range_values(vmax, &beg, &end, exclp)) return Qfalse;
if (begp) *begp = beg;
if (NIL_P(beg)) return Qnil;
if (endp) *endp = end;
if (NIL_P(end)) return Qnil;
return rb_check_funcall_default(end, id_minus, 1, begp, Qfalse);

View file

@ -400,6 +400,7 @@ END
assert_raise(Errno::EDOM, Errno::ERANGE) { r.rand(1.0 / 0.0) }
assert_raise(Errno::EDOM, Errno::ERANGE) { r.rand(0.0 / 0.0) }
assert_raise(Errno::EDOM) {r.rand(1..)}
assert_raise(Errno::EDOM) {r.rand(..1)}
r = Random.new(0)
assert_in_delta(1.5488135039273248, r.rand(1.0...2.0), 0.0001, '[ruby-core:24655]')