# Rails console

**URL:** <https://www.ruby-forum.com/t/rails-console/152155>\
**Category:** Rails\
**Created:** [October 30, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/rails-console/152155 "2008-10-30T17:23:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farrel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bcef8e/32.png) [@Farrel](https://www.ruby-forum.com/u/Farrel)\
**Post date:** [October 30, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/rails-console/152155/1 "2008-10-30T17:23:31Z")

</div>

I tried to create an addition to Enumerable called choose\_randomly  
that picks a random element from the Enumerable object. However when I  
define it and execute the code rand complains about the wrong number  
of arguments. Below is a capture of the ./script/console session.

farrel@nicodemus ~/Work/GreenGreen/Code/green\_green\_client\_application  
$ ./script/console  
Loading development environment (Rails 2.1.2)

> > [1,2,3].choose\_randomly  
> > NoMethodError: undefined method `choose\_randomly’ for [1, 2, 3]:Array  
> > from (irb):1
> 
> > module Enumerable  
> > def choose\_randomly  
> > array\_of\_self = Array( self )  
> > index = rand( array\_of\_self.size )  
> > array\_of\_self[index]  
> > end  
> > end  
> > =\> nil
> 
> > [1,2,3].choose\_randomly  
> > ArgumentError: wrong number of arguments (1 for 0)  
> > from (irb):5:in `rand' from (irb):5:in`choose\_randomly’  
> > from (irb):9
> 
> > [1,2,3][rand( [1,2,3].size )]  
> > =\> 1

As you can see if I execute the code myself it seems to work. If I  
copy that same code into irb it works perfectly.

Any ideas?

Farrel

---

<div class="post-metadata">

**Author:** ![Farrel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bcef8e/32.png) [@Farrel](https://www.ruby-forum.com/u/Farrel)\
**Post date:** [October 30, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/rails-console/152155/2 "2008-10-30T18:06:23Z")

</div>

On 30 Oct 2008, at 16:22, Farrel wrote:

> As you can see if I execute the code myself it seems to work. If I  
> copy that same code into irb it works perfectly.

You’re calling Array#rand (a private method Array defines), whereas  
you want to call Kernel#rand. Replace rand with Kernel.rand and you  
should be ok

Fred

---

<div class="post-metadata">

**Author:** ![Farrel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bcef8e/32.png) [@Farrel](https://www.ruby-forum.com/u/Farrel)\
**Post date:** [October 30, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/rails-console/152155/3 "2008-10-30T19:42:40Z")

</div>

On Oct 30, 7:05 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> You’re calling Array#rand (a private method Array defines), whereas  
> you want to call Kernel#rand. Replace rand with Kernel.rand and you  
> should be ok

That was it. Thanks for the tip. I didn’t know Rails added in it’s own  
rand method into Array.

Farrel
