What does || exactly Mean? [duplicate] - ruby

This question already has answers here:
Understanding the "||" OR operator in If conditionals in Ruby
(9 answers)
Closed 7 years ago.
Recently I saw code like
i < 0 ||
I wonder what is "||" exactly mean? How to say that in english?

Logical "or"
A || B is true when either A is true or B is true, or when both A and B are true.
http://www.tutorialspoint.com/ruby/ruby_operators.htm

The || operator is similar to the keyword or but is different from the keyword or in extremely important ways. Below are two great write-ups on the topic, comparing the two and showing you how to use either one:
http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/
New version, with video: http://devblog.avdi.org/2014/08/26/how-to-use-rubys-english-andor-operators-without-going-nuts/
The most important thing to note in what Avdi says, is that || cannot be used for flow control, whereas or can be.
For example...
a = :value
c = b || a
#de Since `b` is undefined/null, `c` will be set to `:value`
c = b || puts("Failure!") #de This will raise an exception!
c = b or puts("Failure!") #de Will set `c` to `NilClass` and output "Failure!"

Related

Ruby question mark operator, what does this mean? [duplicate]

This question already has answers here:
How do I use the conditional operator (? :) in Ruby?
(7 answers)
Closed 1 year ago.
this might be a very vague question. But i am wondering if someone could translate this into pseudocode:
a = (1 + (bool ? rand(13) : 0)
Does it mean that a will become any value between 0-13 + 1? what is the purpose of the boolean value and the question mark?
(true ? rand(13) : 0) mean (if true then rand(13) else 0 end)
if you have directly "true" in condition, the "else" is never called (is useless), you can write : a = 1 + rand(13) directly ;)
rand(13) give random int between 0 and 12 ;)
if you want "13" put rand(14)
personally I always use range like this (all range is include, it's easier to understand) : rand(0..13)

Ruby chaining methods with if-statement [duplicate]

This question already has an answer here:
One line if statement in Ruby
(1 answer)
Closed 8 years ago.
I have the following:
def method(integer)
a = 3+integer
a += 10 if "one"<"another"
end
Can I write it in one line somehow with chaining methods?
Something like a = 3+f += 10 if "one"<"another"?
You could do it in one line using the ternary operator:
def method(integer)
a = integer + ("one"<"another" ? 13 : 3)
end
Make sure you don't hurt the readability of the code when you do that, though.
Since and or && both use short-circuit evaluation, you could use:
(a = 3+integer) and ("one"<"another") and (a += 10)
It says in 'Using “and” and “or” in Ruby':
and is useful for chaining related operations together until one of them returns nil or false
Another way of thinking about and is as a reversed if statement modifier
a= 3+ integer + ("one"<"another" ? 10 : 0)
3+ integer will add 3 to integer value and ("one"<"another" ? 10 : 0) will return 10 if condition is true otherwise will return 0.

Ruby ++ not work? [duplicate]

This question already has answers here:
No increment operator (++) in Ruby? [duplicate]
(3 answers)
Closed 9 years ago.
Using Ruby, I can't seem to get the following to work:
a = 1
a++
The above line works in irb but doesn't work when I compile from file.
Is there anything I missed out? I'm using Ruby 2.0.
Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1
Ruby doesn't have ++ or -- operators but += and -= accomplish the same thing. Try using the += notation like this:
a = 1
a+= 1
#=> 2
Here is a good reference list of valid ruby operators.

Complex IF THEN statements in VB6 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Does VB6 short-circuit complex conditions?
I am curious about how IF statements are executed in VB6.
For example if I have the statement
If x And y Then
'execute some code
End If
Does the code move on if x is not true? Or does it go ahead and evaluate y even though there is no logical point?
Another example
If x Or y Then
'execute some code
End If
Does the code continue and evaluate y if x is true?
EDIT:
Is there a way to avoid nested IF statements if I want to evaluate very complex conditions and I don't want to waste CPU time?
What you are describing is short circuiting logic, and VB6 doesn't have it...
For example, in VB.Net you might write
If x AndAlso y then...
In this case y is not tested if x turns out to be false.
In your VB6 example, you'll get a Object or With block variable not set error if you try something such as:
Dim x as Object
If Not x Is Nothing And x.y=1 Then
Since object x has not been instantiated.
An unwieldy or-like statement that exhibits short circuiting behaviour:
select case True
case a(), b(), c()
'//if a returns true b & c are not invoked, if b returns true a & b were invoked
case else
...
To answer your edit - avoiding nested IF statements, you can use Select Case, covered in the latter half of this article.
Code snippet from the article:
Select Case strShiftCode
Case "1"
sngShiftRate = sngHourlyRate
Case "2"
sngShiftRate = sngHourlyRate * 1.1
Case "3"
sngShiftRate = sngHourlyRate * 1.5
Case Else
Print "Shift Code Error"
End Select

What is the difference between 'and, or' and '&&, ||'? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
PHP - and / or keywords
Ruby: difference between || and 'or'
What is the difference between this:
a=20
b=30
if (a > 10 && b > 10)
puts a
end
and this:
if (a > 10 and b > 10)
puts a
end
and also explain difference between "||" and "or"
The answer relates to the PHP tag that has been assigned to this question, but has been deleted afterwards
They are the same with one small difference. The and and or operator have a lower precedence compared to && and ||.
Take this example from the manual:
// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;
// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;
You can read about operators in PHP's documentation.
Only the difference in precedence
As you can see from the precedence table for Ruby, the difference is also precedence, as it is in PHP: and and or have lower precedence than && and ||.
But there is a slight difference from PHP: in Ruby, and and or have the same precedence.
I got the answer. Please go and read it. this is what i'm expect it.
http://avdi.org/devblog/2010/08/02/using-and-and-or-in-ruby/
Thanks all of your efforts

Resources