Why does single `=` work in `if` statement?
The only necessary thing for an if
statement to be valid is a boolean expression. In this case, since =
returns the result of the assignment, what's actually being tested is the falsiness of session["devise.facebook_data"]
.
IntelliJ has a good point to lodge a complaint about code like this, as it's difficult to read without knowing a thing or two about Ruby. A recommendation would be to move that to an explicit assignment statement instead. This has the added benefit of DRYing up a reference to it twice.
class User < ActiveRecord::Base
def self.new_with_session(params, session)
super.tap do |user|
data = session["devise.facebook_data"]
if data && data["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
end
How does the single equal sign work in the if statement in javascript
The first part of your analysis is of course correct.
Now, the interesting part might be why your last code if (var ...) {
doesn't work.
It doesn't work because
1)
var something
is a statement, not an expression.
2) here's how ECMAScript defines the if statement :
IfStatement :
if ( Expression ) Statement else Statement
if ( Expression ) Statement
You must put an expression in the if
clause, not a statement.
More on expressions vs statement in this article.
A php if statement with one equal sign...? What does this mean?
It's a form of shorthand, which is exactly equivalent to this:
$confirmation = $payment_modules->confirmation();
if ($confirmation) {
}
If statement always giving the same answer
You are assigning the value true to enoughtreats.
Try using the equality operator rather than assignment:
if (enoughtreats == true) {
...
}
or simply:
if(enoughtreats) {
...
}
What does = instead of == mean in c if statement?
=
is an assignment operator
in C. According to C99 6.5.16:
An assignment operator stores a value in the object designated by the
left operand. An assignment expression has the value of the left
operand after the assignment, but is not an lvalue.
It means that expression a = 5
will return 5
and therefore instructions inside if
block will be executed. In contrary, if you replaced it with a = 0
then 0
would be returned by assignment expression and instructions inside if
would not be executed.
Use and meaning of in in an if statement?
It depends on what next
is.
If it's a string (as in your example), then in
checks for substrings.
>>> "in" in "indigo"
True
>>> "in" in "violet"
False
>>> "0" in "10"
True
>>> "1" in "10"
True
If it's a different kind of iterable (list, tuple, set, dictionary...), then in
checks for membership.
>>> "in" in ["in", "out"]
True
>>> "in" in ["indigo", "violet"]
False
In a dictionary, membership is seen as "being one of the keys":
>>> "in" in {"in": "out"}
True
>>> "in" in {"out": "in"}
False
can you have two conditions in an if statement
You can use logical operators
to combine your boolean expressions
.
&&
is a logical and (both conditions need to betrue
)||
is a logical or (at least one condition needs to betrue
)^
is a xor (exactly one condition needs to betrue
)- (
==
compares objects by identity)
For example:
if (firstCondition && (secondCondition || thirdCondition)) {
...
}
There are also bitwise operators:
&
is a bitwise and|
is a bitwise or^
is a xor
They are mainly used when operating with bits and bytes
. However there is another difference, let's take again a look at this expression:
firstCondition && (secondCondition || thirdCondition)
If you use the logical operators and firstCondition
evaluates to false
then Java
will not compute the second or third condition as the result of the whole logical expression is already known to be false
. However if you use the bitwise operators then Java
will not stop and continue computing everything:
firstCondition & (secondCondition | thirdCondition)
JavaScript - returning true even though condition is not fulfilled
The condition is always true
, because you assign this value to the variable and this is the value which is evaluated for the if
clause.
But you could use a direct check without a compare value (and without assigning this value).
Beside that, you could change the else
part to only an if
part, becaue you exit the function with return
, so no more else
happen in this case.
if (usernameExists) {
console.log("returning true");
return true;
}
if (looped) {
console.log(usernameExists+" is returned");
looped = null;
return false;
}
Are double square brackets [[ ]] preferable over single square brackets [ ] in Bash?
[[
has fewer surprises and is generally safer to use. But it is not portable - POSIX doesn't specify what it does and only some shells support it (beside bash, I heard ksh supports it too). For example, you can do
[[ -e $b ]]
to test whether a file exists. But with [
, you have to quote $b
, because it splits the argument and expands things like "a*"
(where [[
takes it literally). That has also to do with how [
can be an external program and receives its argument just normally like every other program (although it can also be a builtin, but then it still has not this special handling).
[[
also has some other nice features, like regular expression matching with =~
along with operators like they are known in C-like languages. Here is a good page about it: What is the difference between test, [
and [[
? and Bash Tests
Related Topics
Accessing a Ruby Hash with a Variable as the Key
Gmaps4Rails:Setting Map Width and Height
How to Cancel Evaluating a Required Ruby File? (A.K.A. Top-Level Return)
What Is an Eoferror in Ruby File I/O
Defined' and 'Unless' Not Working as Expected
Why Can't We Override '||' and '&&'
How to Override the System Timezone in Ruby
Expressing Conditional Haml Possibly with Ternary Operator
Validates Presense VS Null False in Rails Models/Tables
How to Escape the Vertical Bar When Doing Command Line with Ruby
How to Speed Up Ruby/Rake Task
Currying a Proc with Keyword Arguments in Ruby