What does !!some_object do? [duplicate] - ruby-on-rails

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does !! mean in ruby?
what is this function doing?
def current_product?
!!current_product
end
Isn't that a double negative?

!! is basically a cast to boolean. If current_product is truthy, !current_product is false and !!current_product is true, and vice versa. I.e. it converts truthy values to true and falsy values to false.

It's effectively a cast/conversion to boolean.
Similar question, but for C++: Doube Negation in C++ code
Also a pretty decent post about it here: !! (The double bang / double not) in Ruby

This is a pattern you'll see in any language where every object has a truth value, but there are canonical booleans (whether they be called True and False, 1 and 0, 1 and "", t and nil, whatever). !!x is essentially a "cast to boolean", in that !!x has the same truth-value as x, but !!x will always be one of the canonical true/false values, instead of any old true/false value.

Related

Lua nonsense to find if something is nil

There seems to be a difference between these two checks:
if not object then
if type(object) == "nil" then
However I really don't understand the difference.
if type is "nil", shouldn't a not object then also work?
Lua, like many other dynamically typed languages, has the concept of "truthy" and "falsy", where boolean expressions can handle more values than just actual booleans.
Each non-boolean value has a specific meaning attached when used in a boolean expression. Specifically:
nil and false are "falsy"
everything else is "truthy"
That is why (not nil) == (not false), but type(nil) ~= type(false), because not x is a boolean expression that coerces x to truthy/falsy, while type() checks the actual type.

Why does "not nil" return true in Lua?

I have used repl.it to see what it returned, just for the sake of curiosity, and it turned out that
not nil
returnes true
Why is it? Is it because in Lua everything should be rather true or false in the end?
Repl.it link: https://repl.it/repls/SanePastelHarrier
Because nil is false when converted to boolean:
2.2 Booleans
The boolean type has two values, false and true, which represent the traditional boolean values. However, booleans do not
hold a monopoly of condition values: in Lua, any value may represent a
condition. Conditionals (such as the ones in control structures)
consider both false and nil as false and anything else as true.
Beware that, unlike some other scripting languages, Lua considers both zero
and the empty string as true in conditional tests.
And not treats its argument as a boolean:
3.3 Logical Operators
The logical operators areand, or, and not. Like control structures, all logical operators consider both false and nil
as false, and anything else as true.

Result values in '? :' expression have mismatching types '()' and 'Bool' [duplicate]

This question already has answers here:
Swift ternary operator compilation error
(2 answers)
Closed 6 years ago.
I have an array of Doubles, and a button which when pressed empties the array. I want the button to be enabled only when the count of the array is greater than zero. The code is the following:
var numbers: [Double] = [] //At some point I add some numbers here
numbers.count > 0 ? deleteAllNumbersButton.isEnabled = true : deleteAllNumbersButton.isEnabled = false
The compiler complains:
Result values in '? :' expression have mismatching types '()' and
'Bool'
When put in an if statement it works just fine though. I can't understand the issue here. Has anyone seen this before? I use XCode 8.2.1 and Swift 3.
Note, I don't know Swift, but this doesn't appear to be a Swift specific problem. I can't explain the exact error, but I can show you how to write it properly.
Conditional expressions are used almost exclusively when you need to assign something to a variable or return a value, and have exactly 2 options to choose from.
This is what you're trying to do, but you've written it in a convoluted way that's likely confusing the compiler.
In the expression:
numbers.count > 0 ? deleteAllNumbersButton.isEnabled = true
: deleteAllNumbersButton.isEnabled = false
Because the "then" and "else" expressions both contain assignments, they evaluate (I'm assuming) to a Unit (())/"void". I'm guessing this is why it's yelling at you. It never makes sense to use a ternary to return a Unit (Actually, as noted in the comments, operator precedence is the real reason for the error).
What you likely meant was:
deleteAllNumbersButton.isEnabled = numbers.count > 0 ? true : false
Notice how instead of assigning in the conditional expression, the result of the expression is instead assigned. In cases that can't be simplified further (see below), this is how conditional expressions should be used.
This new form should raise red flags though. Why have the conditional expression evaluate to true/false? That's almost always a code smell. It's redundant given the condition already evaluates to a Boolean value.
Just reduce it down to:
deleteAllNumbersButton.isEnabled = numbers.count > 0

What is the "||=" construct in Ruby? [duplicate]

This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 8 years ago.
In my readings about structuring methods with options hashes for ruby, I've run into a coding "motif" a few times that I can't explain. Since I don't know what it's called, I'm having a lot of difficulty looking it up to learn more about it.
Here's an example:
1 def example_method(input1, options={})
2
3 default_options = {
4 :otherwise => "blue",
5 :be_nil => nil
6 }
7
8 options[:be_nil] ||= options[:otherwise]
9
10 # other code goes down here
11 end
So above, on line 8, you can see what I'm talking about. From what I can put together, the line of code acts similarly to a tertiary operator. Under one condition, it sets a variable to one value... under a different condition, it sets the variable to a different value. In this case, however, the code updates a hash that's stored in the "options" variable. Is that a correct assumption? Furthermore, what is this style/operator/functionality called?
THis is a conditional assignment. The variable on the left will be assigned a value if it is nil or false. This is the short for of saying:
unless options[:be_nil]
options[:be_nil] = options[:otherwise]
end

how to compare if float variable value is infinite? [duplicate]

This question already has an answer here:
Check for inf - Objective-C
(1 answer)
Closed 9 years ago.
debugger says me float value is inf
I just see in debug window section of xcode is this
floatVar1=(float)inf
inf means infinite ?
so how can I compare it ?
something like this:
if (floatVar1==INFITINE){
[self doBlah];
}
To test whether a value is either positive infinity or negative infinity:
if (isinf(floatVar1)) …
To test only whether a value is positive infinity:
if (floatVar1 == INFINITY) …
In either case, use #include <math.h>.
I guess this should work, though it's not objective-c put plain c:
if(isinf(floatVar1)) { ... }
Also, you need to include math.h. For more info see http://www.cplusplus.com/reference/cmath/isinf/

Resources