[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

# arguments to rand

d c

2/26/2008 2:14:00 PM

hi -

can someone help with suggesting what is wrong below? i am trying to
add a simple method to array#random

but get the wrong number of args error...
have tried lots of variations!

>> class Array
>> def random
>> n = rand(self.size.to_i)
>> return self[n]
>> end
>> end
=> nil
>> [1, 2, 3].random
ArgumentError: wrong number of arguments (1 for 0)
from (irb):14:in `rand'
from (irb):14:in `random'
from (irb):18
>> rand(3)
=> 1
>> rand(3)
=> 1
>> rand(3)
=> 2


tx

/dc

8 Answers

Stefano Crocco

2/26/2008 2:20:00 PM

0

Alle Tuesday 26 February 2008, d c ha scritto:
> hi -
>
> can someone help with suggesting what is wrong below? i am trying to
> add a simple method to array#random
>
> but get the wrong number of args error...
> have tried lots of variations!
>
> >> class Array
> >> def random
> >> n = rand(self.size.to_i)
> >> return self[n]
> >> end
> >> end
>
> => nil
>
> >> [1, 2, 3].random
>
> ArgumentError: wrong number of arguments (1 for 0)
> from (irb):14:in `rand'
> from (irb):14:in `random'
> from (irb):18
>
> >> rand(3)
>
> => 1
>
> >> rand(3)
>
> => 1
>
> >> rand(3)
>
> => 2
>
>
> tx
>
> /dc

It works for me. By the way, you don't need the to_i in the call to rand:
Array#size is already and integer.

Stefano

Thomas Wieczorek

2/26/2008 2:42:00 PM

0

On Tue, Feb 26, 2008 at 3:14 PM, d c <lister@pikkle.com> wrote:
> hi -
>
> can someone help with suggesting what is wrong below? i am trying to
> add a simple method to array#random
>
> but get the wrong number of args error...
> have tried lots of variations!
>
> >> class Array
> >> def random
> >> n = rand(self.size.to_i)
> >> return self[n]
> >> end
> >> end
> => nil
> >> [1, 2, 3].random
> ArgumentError: wrong number of arguments (1 for 0)
> from (irb):14:in `rand'
> from (irb):14:in `random'
> from (irb):18
> >> rand(3)
> => 1
> >> rand(3)
> => 1
> >> rand(3)
> => 2
>
>
> tx
>
> /dc
>
>

Works for me.

Calamitas

2/26/2008 2:53:00 PM

0

On 26/02/2008, d c <lister@pikkle.com> wrote:
> hi -
>
> can someone help with suggesting what is wrong below? i am trying to
> add a simple method to array#random
>
> but get the wrong number of args error...
> have tried lots of variations!
>
> >> class Array
> >> def random
> >> n = rand(self.size.to_i)

You are probably calling Array#rand as defined by Rails. If so, it
does what your Array#random is supposed to do and you can use it
directly. Otherwise you can call Kernel.rand instead of just rand.

Peter

Jari Williamsson

2/26/2008 3:11:00 PM

0

d c wrote:
> can someone help with suggesting what is wrong below?

You're monkey patching a built-in class. ;-)


Best regards,

Jari Williamsson


> i am trying to
> add a simple method to array#random
>
> but get the wrong number of args error...
> have tried lots of variations!
>
>>> class Array
>>> def random
>>> n = rand(self.size.to_i)
>>> return self[n]
>>> end
>>> end
> => nil
>>> [1, 2, 3].random
> ArgumentError: wrong number of arguments (1 for 0)
> from (irb):14:in `rand'
> from (irb):14:in `random'
> from (irb):18
>>> rand(3)
> => 1
>>> rand(3)
> => 1
>>> rand(3)
> => 2
>
>
> tx
>
> /dc
>
>

Stefan Rusterholz

2/26/2008 3:45:00 PM

0

David Collier wrote:
>>> class Array
>>> def random
>>> n = rand(self.size.to_i)
>>> return self[n]
>>> end
>>> end
> => nil
>>> [1, 2, 3].random
> ArgumentError: wrong number of arguments (1 for 0)
> from (irb):14:in `rand'

Did you by any chance alias random to rand in Array? If so then that
would be the reason.
Anyway, maybe you can use this one, it's what I use for Array#random:

class Array
# Get a random element of this array
# With an argument it gets you n elements without any index used more
than once
def random(n=nil)
unless n then
at(rand(length))
else
raise ArgumentError unless Integer === n and n.between?(0,length)
ary = dup
l = length
n.times { |i|
r = rand(l-i)+i
ary[r], ary[i] = ary[i], ary[r]
}
ary.first(n)
end
end
end

--
Posted via http://www.ruby-....

Yossef Mendelssohn

2/26/2008 4:14:00 PM

0

On Feb 26, 8:52 am, Calamitas <calamita...@gmail.com> wrote:
> On 26/02/2008, d c <lis...@pikkle.com> wrote:
>
> > hi -
>
> > can someone help with suggesting what is wrong below? i am trying to
> > add a simple method to array#random
>
> > but get the wrong number of args error...
> > have tried lots of variations!
>
> > >> class Array
> > >> def random
> > >> n = rand(self.size.to_i)
>
> You are probably calling Array#rand as defined by Rails. If so, it
> does what your Array#random is supposed to do and you can use it
> directly. Otherwise you can call Kernel.rand instead of just rand.
>
> Peter

This is what I was going to bring up. I'd also like to add that it was
a wonderful decision on Norbert Crombach's part to name the method
Array#rand rather than Array#random so that confusion and/or annoyance
would set in were one to attempt to add another method to Array that
attempts to use random numbers. Oh, also the Rails core team in
general for not bringing this up and accepting the patch without a
comment.

--
-yossef

The Peeler

6/9/2013 9:00:00 PM

0

On Sun, 09 Jun 2013 13:38:57 -0700, The Rectum, the resident psychopath of
sci and scj, FAKING his time zone again and IMPERSONATING his master, The
Peeler, wrote:

>> Gooney Goooooooran Radavich the Serb nose - picker has company over
>> this weekend ..... ... fleas, bedbugs, cockroaches, assorted rodents &
>> a Croat!
>
> Fart out the singular of "allies" Bumsky! LOL

...."& a Croat"!!!!!!!! LMAO! Are you still trying to make a baby together
with Stupid DoDo, "The Rectum"? LOL

--
Our resident psychopath, "The Rectum", addressing his Croatian lover, Stupid
DoDo:
"We're always, greeking each, other in the anus, innit!"
MID: <d4dbp8h9crkcsj9q30nqjo4nujpjm4qgdt@4ax.com>

The Peeler

6/9/2013 11:47:00 PM

0

On Sun, 9 Jun 2013 22:59:44 +0200, The Peeler
<finishingoff@themoronicRevd.invalid> wrote:

>..... ... fleas, bedbugs, cockroaches, assorted rodents &
>>> a jew!
>>
>> Fart out the singular of "allies" Bumsky! LOL
>
>..."& a Jewish person"!!!!!!!! LMAO! Are we still trying to make a baby together
>with Stupid NEMO, "The Rectum"? LOL

Are you Grik anus??? LOL11111