Project

General

Profile

Actions

Feature #4605

closed

Random#randとKernel#randでRangeを扱えるように

Added by yayugu (Yuya Yaguchi) about 13 years ago. Updated about 13 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-dev:43427]

Description

=begin
Random#randとKernel#randで引数としてRangeを扱えるようにしてほしいです。

Random#randは引数にInteger, Float, Rangeを扱うことができるのに対し、Kernel#rand, Random.randではIntegerしか扱うことができません。

rand 10 #=> 7
rand 7.5 #=> 4
rand 10..20 #=> TypeError: can't convert Range into Integer

Random.rand 10 #=> 8
Random.rand 7.5 #=> 3
Random.rand 10..20 #=> TypeError: can't convert Range into Integer

Random.new.rand 10 #=> 8
Random.new.rand 7.5 #=> 6.258043599450456
Random.new.rand 10..20 #=> 20

Floatの扱いを変更すると互換性に問題がでてしまいますが、
Rangeを扱えるようにすることでは問題は少ないと思われますし
rand(10..20)のように記述できることは便利です。
=end


Files

random.diff (6.44 KB) random.diff sorah patch sorah (Sorah Fukumori), 04/24/2011 10:27 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0