Hi,
I have a method that takes a variable length argument list of arrays
i.e.
def meth(*list_of_arrays)
end
now i just want to randomly generate a random number of arrays of random
lengths to pass as arguments to that method (each array being of
integers 0 to 3)
now i just want to randomly generate a random number of arrays of random
lengths to pass as arguments to that method (each array being of
integers 0 to 3)
any suggestions?
One way is to use the built-in function *rand*.
Here is an example program:
arrays = Array.new
rand(10).times do
arr = Array.new
rand(10).times { arr << rand(4) }
arrays << arr
end
thanks, the elegance of these is pleasing. question remains: how to i
split that array of arrays into a list of arrays to pass to the method?
if i don't split it then my method enumerates not through an array of
arrays but through an array with only one member: the array of arrays.
In the end i refactored the method's code to say
if *args.length==1 and args[0].class==Array then args[0].each do x else
args.each do x
but I suppose I'm curious as to whether it's possible to generate a list
of arguments
arrays = Array.new rand 10 do
Array.new(rand 5) { rand 4 }
end
Kind regards
robert
···
On Thu, Oct 21, 2010 at 8:11 AM, Y. NOBUOKA <nobuoka@r-definition.com> wrote:
now i just want to randomly generate a random number of arrays of random
lengths to pass as arguments to that method (each array being of
integers 0 to 3)
any suggestions?
One way is to use the built-in function *rand*.
Here is an example program:
arrays = Array.new
rand(10).times do
arr = Array.new
rand(10).times { arr << rand(4) }
arrays << arr
end
On Thu, Oct 21, 2010 at 8:34 AM, Melody Class <rmiddlehouse@hotmail.com>wrote:
thanks, the elegance of these is pleasing. question remains: how to i
split that array of arrays into a list of arrays to pass to the method?
if i don't split it then my method enumerates not through an array of
arrays but through an array with only one member: the array of arrays.
In the end i refactored the method's code to say
if *args.length==1 and args[0].class==Array then args[0].each do x else
args.each do x
but I suppose I'm curious as to whether it's possible to generate a list
of arguments
That also gives between 0 and 9 arrays, each with between 0 and 4
elements, each element being between 0 and 3.
Note that rand(x) can return 0 so it would be more natural to start
ranges at 0 IMHO.
I did that originally, but (0..0).map { ... } gives a one-element array,
and I wanted to have empty arrays too. You can omit the far end with
(0...0), but then (0...rand(5)) gives between 0 and 3, which is even
more confusing IMO.
Another version with less brackets especially around ranges where I
find them ugly:
That also gives between 0 and 9 arrays, each with between 0 and 4
elements, each element being between 0 and 3.
Note that rand(x) can return 0 so it would be more natural to start
ranges at 0 IMHO.
I did that originally, but (0..0).map { ... } gives a one-element array,
and I wanted to have empty arrays too. You can omit the far end with
(0...0), but then (0...rand(5)) gives between 0 and 3, which is even
more confusing IMO.
No, (0...rand(5)).map... will create arrays with 0 to 4 elements. I
find that totally natural