Ruby Syntax @keywords ||= [ ]

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.

yes… please anyone asnwer the same!

On Thu, Sep 29, 2011 at 02:07:35AM +0900, Bhavesh S. 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.

it creates an empty array, if @keywords isn’t created already

On 2011-09-28, at 1:20 PM, Bhavesh S. wrote:

The ||= operator is exactly what it looks like: “or equals”. That is,

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 S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

Chad P. wrote in post #1024125:

On Thu, Sep 29, 2011 at 02:07:35AM +0900, Bhavesh S. 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.

Awesome ! this should clear my doubts…

On Wed, Sep 28, 2011 at 11:48 PM, Dave A. <

On Wed, Sep 28, 2011 at 13:07, Bhavesh S. [email protected]
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

This would be the same as:
@keywords = @keywords || []

If @keywords has something, use it, else, use []

El 28/09/2011, a las 12:07, Bhavesh S. escribi:

On Thu, Sep 29, 2011 at 02:07:35AM +0900, Bhavesh S. 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.

If @keywords is null then the above would assign the empty array to it.

On Thu, Sep 29, 2011 at 02:07:35AM +0900, Bhavesh S. wrote:
Sorry if this comes across as a dumb question, but what does the
following syntax mean in ruby.

You’ll probably see something like this, too.

(@var ||= []) << ‘val’

Same idea, but appends a value to @var regardless of whether it’s a
brand-new instance variable or not.

-Luke

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs