From: Chad Perrin Date: 2011-08-01T05:27:03+09:00 Subject: Re: A question on style... --YZ5djTAD1cGYuMQK Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Aug 01, 2011 at 05:06:21AM +0900, Joseph Phillips wrote: > Hi everyone, >=20 > Long time developer here, new to Ruby. >=20 > I have a simple, single file Ruby program that randomly chooses a > response to input text. I have a method to return the the array of > responses, which I call further into the file in my main routine. This > is the method: >=20 > def get_responses > =A0=A0=A0 [ > =A0=A0=A0 =A0=A0=A0 "Got it.", > =A0=A0=A0 =A0=A0=A0 "Interesting. I've made a note of that.", > =A0=A0=A0 =A0=A0=A0 "Noted.", > =A0=A0=A0 =A0=A0=A0 "Right.", > =A0=A0=A0 =A0=A0=A0 "I see.", > =A0=A0=A0 =A0=A0=A0 "As you say." > =A0=A0=A0 ] > end >=20 > Is this conventional? Is there any benefit in late acquisition of this > array, or would it be normal to just assign it to a variable up front? All else being equal, I think I would just assign all that to an array: get_response =3D [ 'Got it.', "Interesting. I've made a note of that.", 'Noted.', 'Right.', 'I see.', 'As you say.' ] Unless some other factors apply in context, the main difference is that the method call version has a lot more overhead. With variable assignment, you only have to construct the array once. With the method call version, the method call itself has overhead, and the array is constructed every time the method is called (unless you're using a Ruby implementation that does some aggressive optimization of which I'm not aware right now). There may be times that the method version would be more appropriate, though. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --YZ5djTAD1cGYuMQK Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk41tpAACgkQ9mn/Pj01uKVhTgCeOTKlZLbycR+a9Ar8g96uyZhX 1H0AnA+uqnzNTyiDpSFP8qcJVgiHT135 =i4X3 -----END PGP SIGNATURE----- --YZ5djTAD1cGYuMQK--