I came up with this method really quick to create x*y arrays in Ruby. It
mimicks Array#new's behavior pretty closely wrt blocks, I hope. What do you all
think?
class << Array
def multi(x,y,*args, &block)
if args.length > 0 and block_given?
raise ArgumentError, "wrong number of arguments (#{args.length + 2} for 2)"
elsif args.length > 1 and not block_given?
raise ArgumentError, "wrong number of arguments (#{args.length + 2} for 3)"
end
Array.new(x) do
if block_given?
Array.new(y, &block)
else
Array.new(y, args[0])
end
end
end
end
Usage is:
Array.multi(5,5,0)
# => [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Array.multi(5,5) {""}
# => [["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""]]
···
--
panic ("No CPUs found. System halted.\n");
2.4.3 linux/arch/parisc/kernel/setup.c