From: niklas | brueckenschlaeger Date: 2011-02-24T21:23:52+09:00 Subject: Re: a, b = Array.new(2).map!{|x| data.dup} --=-Nrd5eq+a+jpTFS5jmviE Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, 2011-02-24 at 18:09 +0900, Brian Candler wrote: > Stefan Salewski wrote in post #983443: > > The code is related to handling configuration files, I have a basic > > configuration set, a hash called @output. And variants for on screen, > > pdf, png export. >=20 > In that case, I'd say you'd be better off avoiding all the separate=20 > instance variables for each variant, and make it data-driven. Then you= =20 > can interate over the variants, which will naturally avoid the repeated= =20 > code. >=20 > FORMATS =3D [:scr, :png, :pdf, :svg, :ps] > ... > @configs =3D {} > FORMATS.each { |k| @configs[k] =3D @output.dup } This would be the same as: @configs =3D Hash.new {|h,k| h[k] =3D @output.dup } after that you can use pretty much any key you want :) -- niklas >=20 > Now you have @configs[:scr], @configs[:png] etc, all pointing to=20 > separate copies of @output. And if you want to add another format in=20 > future, it's a trivial change. >=20 > (Aside: please do not post variants of this code using 'inject', because= =20 > it's stupid here. You know who you are. Thank you.) >=20 > Regards, >=20 > Brian. >=20 --=-Nrd5eq+a+jpTFS5jmviE Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iQIcBAABAgAGBQJNZk3LAAoJEIQdQGZOjo82Fu4P/2by286PaNEc5VI+O2zNmxdp nuj1Ph/UZMrBQU7QOjKbCK5gbbOXz6X58c+vjdIjaIrLlzCSnJW0D41KN4IPiPal oJVtnkFM9OJjGHnfu0DAnShWX6p+s45qkBzmQ2sTlhg6LgPHIZGTk8oarkM1cnFi mWr/VpU7J2Ht1W5KvTS+zXbeoaj0I0QAVMmTle/0NNc+GghWQcLBIqnEjoguRxE4 oxJSWrP1aPxbBGdyU8fdQZ+0Sn7i98CcgJ6CvzCJgztPdltroey0NsNI9is5QLAX 97xhrb9Wby9vAm7jCsHXAUg4Lg/mei4Y47rZnOvLTayGrxqpzgKXe0Z2ydxZSw+Z j83ZoX58l1lMZE6J3qp6RGG2CuN29SoU45O8eW25bXlAMmGqPFEvCP1C+zL147ye G54yIbR1/B9YN0+PH5Q2QfBj8Uo8mAxkQdeMs+8MIMECxPvB/jheu+5kGkBGuFTL PbveT5JTHpqBp1/JcbPlOpfArdEQNDPdB1o6R7QhYAx3goPzdryGF6A5H//DVFwo 2xAF58vjiy0lXh3G0LjBojr7/GK7/lAliD9L0H5CrJyggnYfjOL/RMXQmlQFxIkG uRuMMDoG4yM/FGD8afDJjZYDRnIq3xvKqhriawEciflSyvpEezpfzNrFnDjbk3sQ YmWgeWrhh7iwq/3Xx5+X =2lw/ -----END PGP SIGNATURE----- --=-Nrd5eq+a+jpTFS5jmviE--