From: Anthony Martinez Date: 2007-06-25T03:59:09+09:00 Subject: Helper to create multi-dimensional arrays --kr14OxHsRwZHHqxS Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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) =20 # =3D> [[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) {""} # =3D> [["", "", "", "", ""], ["", "", "", "", ""], ["", "", "", "", ""], [= "", "", "", "", ""], ["", "", "", "", ""]] --=20 panic ("No CPUs found. System halted.\n"); 2.4.3 linux/arch/parisc/kernel/setup.c --kr14OxHsRwZHHqxS Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFGfr77KaiGM/xGKzQRApS2AJoD7vFPvqOjYJs2DShP5FFJPjTPDwCgoy5c RKfZw/CmHzE9o1AhY3tfz8Q= =GE3k -----END PGP SIGNATURE----- --kr14OxHsRwZHHqxS--