If(!X) VS If(X==False) in Ruby

if(!x) vs if(x==false) in ruby

Ruby considers that false and nil are the only two "falsy" values, while everything else is "truthy". This is by definition and can not be modified (at least in MRI). This definition is used for all builtin operators like if, unless, while, until, cond ? if_truthy : if_falsey, ||, &&, ...

Writing foo == bar will always call the == method on foo with bar as an argument. By default, nil, false, true and all other immediates like symbols, etc..., are only equal to themselves. This could be changed, though:

def nil.==(bar)
super || bar == false
end
puts "nil == false" if nil == false # => "nil == false"

In Ruby 1.9, you can also redefine the operator !, so unless foo is not necessarily the same as if !foo or the contrary of if foo:

def true.!
true
end

puts "True?" if true # => "True?"
puts "or not?" if !true # => "or not?"

Not that anybody would recommend doing anything like this...

Why does `x =! 5` return false?

This is because x =! 5 is being interpreted as x = (!5) (! has higer precedence than =). In Ruby every object is true except nil and false. 5 has truthy value which you are negating using the operator !. So false as result is being assigned to the local variable x.

! Called Logical NOT Operator - is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make false.

Does @x ||= work like return @x if @x.present?

You should not use either option for memoizing boolean values as both will recalculate if @x is false.

present? is a special kind of rails check that equates to !blank? and false.blank? #=> true but even if this was not a boolean check present? and || are not equivalent. For objects that implement empty? blank defers to that so something that is empty? is also blank? and thus is not present?.

"".present? #=> false
"" || true #=> ""
[].present? #=> false
[] || true #=> []
false.present? #=> false
false || true #=> true

@x ||= some_logic equates to @x = @x || some_logic where obviously if @x is false some_logic will fire.

If you just want to see if @x has already been determined to be a value (e.g. not nil) then you could replace this with

def x? 
return @x unless @x.nil?
@x = some_logic
end

What method is called to evaluate Fixnum equality?

They don't "evaluate to true" in the sense of being equal the object true. All objects except nil and false are considered to be logically true when they're the value of a condition. This idea is sometimes expressed by saying they are truthy.

So, yes, testing equality uses the == operator. A bare 0 is truthy for the same reason 29 and "Hi, I'm a string" are truthy — it's not nil or false.

How does Ruby evaluate math expressions to true/false?

In Ruby only the nil object and a special false object are "false", everything else (including the integer 0) is "true". You should use while (y_now % 4) != 0.

Is if(!x) the same as if(x!=null)

No. if in groovy calls the underlying asBoolean() method. This is known as Groovy truth.

Empty lists, empty strings, empty maps, null, 0, are all falsy values:

if ([:]) {
assert false
}

if (null) {
assert false
}

if ("") {
assert false
}

if (0) {
assert false
}

assert null.asBoolean() == false

assert 1.asBoolean()

You can also write asBoolean in your own classes.

Checking if a variable is not nil and not zero in ruby


unless discount.nil? || discount == 0
# ...
end

Logic equivalent for string comparison

At least first expression does not enter my if

Here is my explanation :

! has higher precedence then ==. So in your expression !x == "string" will be internally represented as (!x) == "string"

!x either will be evaluted to true or false, which are TrueClass or FalseClass object respectively. Now Let's check whose #== method is used by true and false object.

true.method(:==).owner
# => BasicObject
false.method(:==).owner
# => BasicObject

Basic#== : Equality — At the Object level, == returns true only if obj and other are the same object.

As per the above definition your code !x == "string" should always be evaluated to false.

Ruby Conditionals Mechanism

Because puts returns nil:

def test_method
puts "test"
end

t = test_method #=> "test"
p t #=> nil

What that means is if x==10 && nil which will be evaluated to false. Hence it never gets inside the condition and print "hello".



Related Topics



Leave a reply



Submit