# arguments to rand

D

d c

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=> 2


tx

/dc
 
S

Stefano Crocco

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

ArgumentError: wrong number of arguments (1 for 0)
from (irb):14:in `rand'
from (irb):14:in `random'
from (irb):18

=> 1

=> 1

=> 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
 
T

Thomas Wieczorek

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=> 2


tx

/dc

Works for me.
 
C

Calamitas

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!

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
 
J

Jari Williamsson

d said:
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=> 2


tx

/dc
 
S

Stefan Rusterholz

David said:
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 = ary, ary[r]
}
ary.first(n)
end
end
end
 
Y

Yossef Mendelssohn

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top