Ruby |= Assignment Operator

Ruby |= assignment operator

Bitwise OR assignment.

x |= y

is shorthand for:

x = x | y

(just like x += y is shorthand for x = x + y).

What does ||= (or-equals) mean in Ruby?

This question has been discussed so often on the Ruby mailing-lists and Ruby blogs that there are now even threads on the Ruby mailing-list whose only purpose is to collect links to all the other threads on the Ruby mailing-list that discuss this issue.

Here's one: The definitive list of ||= (OR Equal) threads and pages

If you really want to know what is going on, take a look at Section 11.4.2.3 "Abbreviated assignments" of the Ruby Language Draft Specification.

As a first approximation,

a ||= b

is equivalent to

a || a = b

and not equivalent to

a = a || b

However, that is only a first approximation, especially if a is undefined. The semantics also differ depending on whether it is a simple variable assignment, a method assignment or an indexing assignment:

a    ||= b
a.c ||= b
a[c] ||= b

are all treated differently.

When is `|=` in Ruby helpful?

For a given operator op, something like:

a op= b

is more or less shorthand for:

a = a op b

What | means is, of course, dependent on what you're |-ing:

  • For Integer, it is a bit-wise OR.
  • For false, true, and nil, it is a boolean OR.
  • For Array, it is a set-wise union.

In your case:

result[:mentioned_usernames] |= [login]

You're probably working with arrays, the array on the RHS is the give away. The result is that result[:mentioned_usernames] will have login added to it if it isn't there already. There's also a side effect: |= will remove duplicates from result[:mentioned_usernames]; for example:

>> a = [1,2,3,4,4]
=> [1, 2, 3, 4, 4]
>> a |= [1]
=> [1, 2, 3, 4]

||=' operator in Ruby

It's an assignment operator for 'Conditional Assignment'

See here -> http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators

Conditional assignment:

 x = find_something() #=>nil
x ||= "default" #=>"default" : value of x will be replaced with "default", but only if x is nil or false
x ||= "other" #=>"default" : value of x is not replaced if it already is other than nil or false

Operator ||= is a shorthand form of the expression:

x = x || "default" 

EDIT:

After seeing OP's edit, the example is just an extension of this, meaning:

car = method_1 || method_2 || method_3 || method_4

Will assign the first non-nil or non-false return value of method_1, method_2, method_3, method_4 (in that order) to car or it'll retain its old value.

What does ||= mean?

Basically, a ||= b means assign b to a if a is null or undefined or false (i.e. false-ish value in ruby), it is similar to a = b unless a, except it will always evaluate to the final value of a (whereas a = b unless a would result in nil if a was true-ish).

Double Pipe Symbols in Ruby Variable Assignment?

It's a conditional assignment. From here:

 x = find_something() #=>nil
x ||= "default" #=>"default" : value of x will be replaced with "default", but only if x is nil or false
x ||= "other" #=>"default" : value of x is not replaced if it already is other than nil or false

Function name followed by assignment operator


What does the following function definition mean?

They are called writer method in Ruby.

Why there is an assignment operator in the function name?

It adds sugar in your syntax.

While you have a method as

def func=(param)
@param = param
end

You can call it as normal assignment

ob.func = 12 # same as obj.func(12)

Ruby (+=) Add AND assignment operator and initial nil value

If a can ever only be nil or an integer, then

a = a.to_i + 1

What does ||= mean?

It is an assignment operator which means: or assign this value to a variable.

So if you did something like x ||= ythis meansx || x = y so if x is nil or false set x to be the value of y.

Ruby Assignment Operators

Because assignment works on variables not objects and thus cannot be implemented as a method.



Related Topics



Leave a reply



Submit