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.
···
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.
"x ||= y" is a common idiom for "if x is nil, set it to y".
An operator followed by the = sign is like saying "use the thing on
the left, as the left side of the operator". In other words, the
above is equal to:
@keywords = @keywords ||
Similarly, "x += 1" is equal to "x = x + 1", and so on for all other operators.
The || operator specifically won't bother evaluating its right-side
operator, if the left is true. In Ruby, two things are not true:
false, and nil. So, if the thing on the left is nil (or boolean
false), it gets the value on the right, else it gets left alone.
-Dave
···
On Wed, Sep 28, 2011 at 13:07, Bhavesh Sharma <sharmabhavesh@gmail.com> 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.
--
LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web site.
Main Web Site: davearonson.com
Programming Blog: codosaur.us
Excellence Blog: dare2xl.com
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
does this mean that I can create permutations of some operators and it
would still be valid.
On Wed, Sep 28, 2011 at 11:48 PM, Dave Aronson < rubytalk2dave@davearonson.com> wrote:
On Wed, Sep 28, 2011 at 13:07, Bhavesh Sharma <sharmabhavesh@gmail.com> > 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.
"x ||= y" is a common idiom for "if x is nil, set it to y".
An operator followed by the = sign is like saying "use the thing on
the left, as the left side of the operator". In other words, the
above is equal to:
@keywords = @keywords ||
Similarly, "x += 1" is equal to "x = x + 1", and so on for all other
operators.
The || operator specifically won't bother evaluating its right-side
operator, if the left is true. In Ruby, two things are not true:
false, and nil. So, if the thing on the left is nil (or boolean
false), it gets the value on the right, else it gets left alone.
-Dave
--
LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web site.
Main Web Site: davearonson.com
Programming Blog: codosaur.us
Excellence Blog: dare2xl.com
You'll probably see something like this, too. Same idea, but
appends a value to @var regardless of whether it's a brand-new
instance variable or not.
(@var ||= ) << 'val'
-Luke
···
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.
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
···
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.