Created
April 24, 2011 12:40
-
-
Save yayugu/939522 to your computer and use it in GitHub Desktop.
Random#randとKernel#randでRangeを扱えるように
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)のように記述できることは便利です。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment