From: Mike Stok Date: 2011-09-29T02:27:02+09:00 Subject: Re: Ruby Syntax @keywords ||= On 2011-09-28, at 1:20 PM, Bhavesh Sharma wrote: > Chad Perrin wrote in post #1024125: >> 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. >>> >>> @keywords ||= [] >>> >>> I understand that its setting the instance variable as an array but what >>> is the logical operator '||' doing in there. >> >> The ||= 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 ||= []" 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. > > Thanks Chad, > > I didnt see this specific operator on > http://www.tutorialspoint.com/ruby/ruby_operators.htm > > does this mean that I can create permutations of some operators and it > would still be valid. Near the top of this page is the paragraph: For each operator (+ - * / % ** & | ^ << >> && ||), there is a corresponding form of abbreviated assignment operator (+= -= etc.) Once you know what it means it is obvious, but it might not be obvious to a newcomer. They are enumerated in the table at the bottom of the page. Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.