From: Chad Perrin Date: 2011-09-29T02:15:01+09:00 Subject: Re: Ruby Syntax @keywords ||= [ ] --ReaqsoxgOBHFXBhH Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Sep 29, 2011 at 02:07:35AM +0900, Bhavesh Sharma wrote: > Sorry if this comes across as a dumb question, but what does the > following syntax mean in ruby. >=20 > @keywords ||=3D [] >=20 > I understand that its setting the instance variable as an array but what > is the logical operator '||' doing in there. The ||=3D operator is exactly what it looks like: "or equals". That is, because every valid chunk of code in Ruby is an expression, you can read "@keywords ||=3D []" as "give me the keywords variables value, or set it equal to an empty array (and give me that)". Thus, if the contents of @keywords evaluate to "true" (that is, it contains some value other than false or nil), that expression simply evaluates to the value of @keywords. Otherwise, the expression assigns the empty array to @keywords, and evaluates to an empty array. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --ReaqsoxgOBHFXBhH Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk6DVe0ACgkQ9mn/Pj01uKXwEwCfXbpP7R0FciXrXTnUKr2IXW3Z M+cAnihA/BGrf9hFHcSxvAKgayD81Qvw =gVks -----END PGP SIGNATURE----- --ReaqsoxgOBHFXBhH--