Ruby's ||= (or equals) in JavaScript?
Both are absolutely correct, but if you are looking for something that works like ||=
in ruby. The first method which is variable = variable || {}
is the one you are looking for :)
Does Javascript have a conditional assignment like Ruby's ||= (Double pipe)
var x = x || newValue;
This mean whenever x has something being evaluated as false (undefined, null, 0), it will assign the second value.
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.
Bash equivalent of Ruby and JavaScript OR when initializing variables
In bash
you can use the Assign Default Values parameter expansion operator.
: ${a:=1.0}
${parameter:=word}
Ifparameter
is unset or null, the expansion ofword
is assigned toparameter
.
The :
command is a no-op, it's used just so we can perform the parameter expansion in its argument list.
Is there an && Equals operator in Ruby
If you are using ruby
> 2.3.0
you can use the &.
operator: x&.== y
. It basically does what .try
does in Rails
if the value of the operand is different than nil
it calls the method and returns its result. If the value is nil
it returns nil
so you can do things like: do_i_exist&.does_the_result_exist&.nil?
See: What does &. (ampersand dot) mean in Ruby?
Ruby's variable equals if variable equals method?
Yes, it can be confusing, in part because one's first reaction to seeing if identifier = chunk[IDENTIFIER, 1]
may be that it's probably a bug, that the author meant if identifier == chunk[IDENTIFIER, 1]
.
The code block given in the question is probably equivalent to:
tokenizer =
if chunk[IDENTIFIER, 1]
IdentifierTokenizer.new(chunk[IDENTIFIER, 1], tokenizer).tokenize
elsif chunk[CONSTANT, 1]
ConstantTokenizer.new(chunk[CONSTANT, 1], tokenizer).tokenize
elsif chunk[NUMBER, 1]
NumberTokenizer.new(chunk[NUMBER, 1], tokenizer).tokenize
..
Rather than computing chunk[IDENTIFIER, 1]
, chunk[CONSTANT, 1]
and chunk[NUMBER, 1]
twice, those values are assigned to variables (identifier
, constant
, number
) the first time they are calculated. (Those assignments are evaluated before if
and elsif
are applied.) This is commonly done when such calculations are costly or have an undesired side effect when performed more than once. The use of temporary variables in this way also DRYs the code, but other approaches that achieve the same objective may be preferable. I personally avoid such inline variable assignments, in part because I regard them as ugly.
One alternative that may be more clear, and which DRYs the code even more, is the following.
tokenizer =
tokenit(IDENTIFIER, IdentifierTokenize, tokenizer) ||
tokenit(CONSTANT, ConstantTokenize, tokenizer) ||
tokenit(NUMBER, NumberTokenize, tokenizer) ||
...
def tokenit(type, class, tokenizer)
ch = chunk[type, 1]
if ch
class.public_send(:new, ch, tokenizer).tokenize
else
false
end
end
This of course assumes that when ch
is truthy class.public_send(:new, ch, tokenizer).tokenize
is also truthy.
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
Related Topics
Jquery Validate Plugin - How to Create a Simple Custom Rule
What's the Difference Between Event.Stoppropagation and Event.Preventdefault
Detect the Internet Connection Is Offline
How to Post Urlencoded Form Data With $Http Without Jquery
JavaScript Function to Add X Months to a Date
Why Does String to Number Comparison Work in JavaScript
Jquery $(Document).Ready and Updatepanels
Count the Number of Occurrences of a Character in a String in JavaScript
Take a Screenshot of a Webpage With JavaScript
Use of .Apply() With 'New' Operator. Is This Possible
Backslashes - Regular Expression - JavaScript
How to List the Properties of a JavaScript Object
Chained Promises Not Passing on Rejection
Chrome Doesn't Delete Session Cookies
Can't Perform a React State Update on an Unmounted Component
Vertical Centering Variable Height Image While Maintaining Max-Width/Height