Bug #3027

Random#rand(nil)

Added by marcandre (Marc-Andre Lafortune) about 2 years ago. Updated about 1 year ago.

[ruby-core:29075]
Status:Closed Start date:03/28/2010
Priority:Normal Due date:
Assignee:nobu (Nobuyoshi Nakada) % Done:

100%

Category:core
Target version:1.9.2
ruby -v:ruby 1.9.2dev (2010-03-03 trunk 26805) [x86_64-darwin10.2.0]

Description

Hi Nobu.

Looking at Random#rand, I notice that passing nil has the same result as not passing any argument.

Either it should raise an ArgumentError like the documentation implies, or else the documentation should be modified.

I would argue to raise an error because:
- it is easy to pass nothing, or pass 1.0 instead, and get the same result if that is what is desired
- it could hide an error in the code, where the result is unexpectedly nil but the programmer didn't think about it (like whiny nils in rails)

Associated revisions

Revision 27204
Added by nobu (Nobuyoshi Nakada) about 2 years ago

* random.c (random_rand): raise ArgumentError on nil, as the documentation implies. [ruby-core:29075] * random.c (rb_f_rand): mentioned the case of when max is nil.

History

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

Hi,

At Sun, 28 Mar 2010 09:06:57 +0900,
Marc-Andre Lafortune wrote in [ruby-core:29075]:
> Looking at Random#rand, I notice that passing nil has the
> same result as not passing any argument.

It comes from Kernel#rand.

> Either it should raise an ArgumentError like the
> documentation implies, or else the documentation should be
> modified.
> 
> I would argue to raise an error because:
> - it is easy to pass nothing, or pass 1.0 instead, and get
>   the same result if that is what is desired
> - it could hide an error in the code, where the result is
>   unexpectedly nil but the programmer didn't think about it
>   (like whiny nils in rails)

Agreed.  For the backward compatibility, I'll keep Kernel#rand
and added the documentation instead.

-- 
Nobu Nakada

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100
This issue was solved with changeset r27204.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

Also available in: Atom PDF