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

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

Related

What does || exactly Mean? [duplicate]

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!"

Is there a way to represent logical not with && or ||

Is there a way to represent logical not, with && or || or some statement like if or ?:
I want to implement this in some other way:
boolean isRunning = true;
startButton.setEnabled(!isRunning); // <<== ???
Assuming that you are doing this as an exercise, ternary operator lets you replace ! in a simple and straightforward way:
startButton.setEnabled(isRunning ? false : true);
As far as using && and || by themselves goes, this pair of operators is not functionally complete, i.e. there are operations that cannot be implemented by using a sequence of &&s and ||s alone; not ! operation is among operations that cannot be implemented with ands and ors.

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.

Coding styles in conditional expression of some programming languages

It's a bit confusing to me about what is the difference between these condition expressions below:
if( 1 == a) {
//something
}
and
if( a == 1 ) {
//something
}
I saw the above one in some scripts I have downloaded and I wonder what's the difference between them.
The former has been coined a Yoda Condition.
Using if(constant == variable) instead of if(variable == constant), like if(1 == a). Because it's like saying "if blue is the sky" or "if tall is the man".
The constant == variable syntax is often used to avoid mistyping == as =. It is, of course, often used without understanding also when you have constant == function_call_retuning_nothing_modifiable.
Other than that there's no difference, unless you have some weird operator override.
Many programming languages allow assignments like a = 1 to be used as expressions, making the following code syntactically valid (given that integers can be used in conditionals, such as in C or many scripting languages):
if (a = 1) {
// something
}
This is rarely desired, and can lead to unexpected behavior. If 1 == a is used, then this mistake cannot occur because 1 = a is not valid.
Well, I am not sure about the trick. Generally, we could say the equal sign is commutative. So, a = b implies b = a. However, when you have == or === this doesn't work in certain cases, for example when on the right side you have a range: 5 === (1..10) vs. (1..10) === 5.

How to create conditional breakpoint with std::string

Suppose I have this function:
std::string Func1(std::string myString)
{
//do some string processing
std::string newString = Func2(myString)
return newString;
}
How do I set a conditional break when newString has a specific value? (without changing the source)
Setting the condition newString == "my value" didn't work. The breakpoints were disabled with an error overloaded operator not found.
There is a much easier way in Visual Studio 2010/2012.
To accomplish what you are looking for in ANSI use this:
strcmp(newString._Bx._Ptr,"my value")==0
And in unicode (if newString were unicode) use this:
wcscmp(newString._Bx._Ptr, L"my value")==0
There are more things you can do than just a compare, you can read more about it here:
http://blogs.msdn.com/b/habibh/archive/2009/07/07/new-visual-studio-debugger-2010-feature-for-c-c-developers-using-string-functions-in-conditional-breakpoints.aspx
In VS2017, I was able to set the condition as:
strcmp(&newString[0], "my value") == 0
Some searching has failed to turn up any way to do this. Suggested alternatives are to put the test in your code and add a standard breakpoint:
if (myStr == "xyz")
{
// Set breakpoint here
}
Or to build up your test from individual character comparisons. Even looking at individual characters in the string is a bit dicey; in Visual Studio 2005 I had to dig down into the member variables like
myStr._Bx._Buf[0] == 'x' && myStr._Bx._Buf[1] == 'y' && myStr._Bx._Buf[2] == 'z'
Neither of these approaches is very satisfactory. We should have better access to a ubiquitous feature of the Standard Library.
In VS2017 you can do
strcmp(newString._Mypair._Myval2._Bx._Buf,"myvalue")==0
While I've had to work around this using something similar to Brad's answer (plus using DebugBreak() to break right from the code), sometimes editing/recompiling/re-running a bit of code is either too time consuming or just plain impossible.
Luckily, it's apparently possible to spelunk into the actual members of the std::string class. One way is mentioned here -- and though he calls out VS2010 specifically, you can still access individual chars manually in earlier versions. So if you're using 2010, you can just use the nice strcmp() functions and the like (more info), but if you're like me and still have 2008 or earlier, you can come up with a raggedy, terrible, but functional alternative by setting a breakpoint conditional something like:
strVar._Bx._Ptr[0] == 'a' && strVar._Bx._Ptr[1] == 'b' &&
strVar._Bx._Ptr[2] == 'c'
to break if the first three characters in strVar are "abc". You can keep going with additional chars, of course. Ugly.. but it's saved me a little time just now.
VS2012:
I just used the condition below because newString._Bx._Ptr ( as in OBWANDO's answer ) referenced illegal memory
strcmp( newString._Bx._Buf, "my value")==0
and it worked...
#OBWANDO (almost) has the solution, but as multiple comments rightly point out, the actual buffer depends on the string size; I see 16 to be the threshold. Prepending a size check to the strcmp on the appropriate buffer works.
newString._Mysize < 16 && strcmp(newString._Bx._Buf, "test value") == 0
or
newString._Mysize >= 16 && strcmp(newString._Bx._Ptr, "ultra super long test value") == 0
Tried to use strcmp in gdb8.1 under ubuntu18.04, but it doesn't work:
(ins)(gdb) p strcmp("a", "b")
$20 = (int (*)(const char *, const char *)) 0x7ffff5179d60 <__strcmp_ssse3>
According to this answer, strcmp, is a special IFUNC, one can setup condition like this:
condition 1 __strcmp_ssse3(camera->_name.c_str(), "ping")==0
It's pretty ugly, don't want to do it the second time.
This answer gives a much better solution, it use std::string::compare :
condition 1 camera->_name.compare("ping") == 0
In VS2015 you can do
newstring[0]=='x' && newString[1]=='y' && newString[2]=='z'
Comparing string works better than comparing characters
strcmp(name._Mypair._Myval2._Bx._Buf, "foo")==0
This works, but is very inconvenient to use and error prone.
name._Mypair._Myval2._Bx._Buf[0] == 'f' &&
name._Mypair._Myval2._Bx._Buf[1] == '0' &&
name._Mypair._Myval2._Bx._Buf[2] == '0'
You could convert it into a c string using c_str() like so:
$_streq(myStr.c_str(), "foo")
To set a conditional breakpoint in std::string you need to set it on real internal members of std::string. What you see on watch window is simplified.
You can display real structure of a variable in the watch window by using ,! suffix. In your example:
newString,!
For MSVC 2015 – 2019 you can use:
For string that were never longer than 15 characters:
(newString._Mypair._Myval2._Myres < 16) ?
strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 :
false
For (even historically) longer strings:
(newString._Mypair._Myval2._Myres < 16) ? false :
strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0
Beware:
The variable name is written twice in each condition!
You need whole expression on single line. Use the copy-paste versions bellow.
Universal condition needs to put the test value twice and variable name three times:
(newString._Mypair._Myval2._Myres < 16) ?
strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 :
strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0
Notes: use wcscmp instead of strcmp if you are working with std::wstring.
Find more info on small string optimization in C++ https://vorbrodt.blog/2019/03/30/sso-of-stdstring/ includes sample code to find size of string's internal buffer.
All std:string and std::wstring single line versions for your copy paste convenience:
(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 : false
(newString._Mypair._Myval2._Myres < 16) ? false : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0
(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0
(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"short") == 0 : false
(newString._Mypair._Myval2._Myres < 16) ? false : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_str_value_longer_than_16_chars") == 0
(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"My_test_string") == 0 : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_string") == 0
All above copy/paste samples tested on MSVC version 16.9.10 and program for Windows 10.

Resources