From: Bhavesh Sharma Date: 2011-09-29T02:20:03+09:00 Subject: Re: Ruby Syntax @keywords ||= 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. -- Posted via http://www.ruby-forum.com/.