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.

···

--
Posted via http://www.ruby-forum.com/.

yes... please anyone asnwer the same!

···

On Wed, Sep 28, 2011 at 10:37 PM, 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.

--
Posted via http://www.ruby-forum.com/\.

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.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

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

···

--
Posted via http://www.ruby-forum.com/.

"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

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 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.

--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

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 Sharma escribió:

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.

--
Posted via http://www.ruby-forum.com/\.

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

does this mean that I can create permutations of some operators and it
would still be valid.

--
Posted via http://www.ruby-forum.com/\.

Awesome ! this should clear my doubts..

···

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.

--
Posted via http://www.ruby-forum.com/\.

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

···

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.

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.