From: Anthony Martinez Date: 2007-06-25T09:18:37+09:00 Subject: Re: Helper to create multi-dimensional arrays --UlsYxwg8UDQn+EKZ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Jun 25, 2007 at 08:06:44AM +0900, Daniel DeLorme wrote: > Anthony Martinez wrote: > >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? >=20 > What about making it more general than just x*y arrays? Ooh. That didn't occur to me. Nice. >=20 > class << Array > def multi(n, *args, &block) > if args.empty? > Array.new(n, &block) > else > Array.new(n) do > Array.multi(*args, &block) > end > end > end > end >=20 > ?> Array.multi(2){ 0 } > =3D> [0, 0] > >> Array.multi(2,2){ 0 } > =3D> [[0, 0], [0, 0]] > >> Array.multi(2,2,2){ 0 } > =3D> [[[0, 0], [0, 0]], [[0, 0], [0, 0]]] > >> Array.multi(2,2,2,2){ 0 } >=20 >=20 >=20 > hehe, playing around is fun >=20 > Daniel >=20 --=20 How'd you get this number? -- A Qwest Central Office Technician --UlsYxwg8UDQn+EKZ Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFGfwndKaiGM/xGKzQRAil1AJ9JI6MIaEqu6sWe15AOB+rSSgez8gCguGOc eHyT3a55RCoV15xSWxg9x0Y= =JFyL -----END PGP SIGNATURE----- --UlsYxwg8UDQn+EKZ--