[Ruby] If and else problems-with operators-

Problem: You are a policeman.
I am investigating two suspects, a and b.
In both cases, if the testimony is True or False, the testimony is True.
However, if either is False and True, the testimony is False, and let's create a method that outputs it using a logical operator.


** How to call: ** police_trouble(a, b)
** Output example: **

police_trouble(true, false) → False
police_trouble(false, false) → True
police_trouble(true, true) → True

** Logical operator ** Logical operators to describe complex conditional expressions that combine two or more conditional expressions&&、||、!Is used. Use it as follows.
#true if both a and b are true
a && b

#true if either a or b is true
a || b 

#false if a is true, true if a is false
!a





**answer**
def police_trouble(a, b)
  if (a && b) || (!a && !b)
    puts "True"
  else
    puts "False"
  end
end

police_trouble(true, false) 
police_trouble(false, false) 
police_trouble(true, true) 

Logical operator in if statement&&When||When!Describe the conditional expression using a. 「(a && b) || (!a && !b)"Is ① Both a and b are true ②    〃  false In either case, "** True **" is output. Otherwise (if not meshing) output False.

Recommended Posts

[Ruby] If and else problems-with operators-
[Ruby] present/blank method and postfix if.
[Ruby] Boolean values ​​and logical operators
if else practice
[Ruby] postfix if
Ruby and Gem
Ruby if, case
[Ruby] Classes and instances
Symbols and Destructive Ruby
[Ruby] Big Decimal and DECIMAL
7th class: if, else
Ruby Learning # 17 If Statements
About Ruby if statement
Comparison operators and conditionals
Ruby classes and instances
Ruby inheritance and delegation
[Ruby] Difficulty in refactoring logical operators (accuracy and readability)
Ruby variables and methods
Basic operators and operations
About for statement and if statement
GraphQL Ruby and actual development
Ruby Learning # 18 If Statements (Con't)
Java if and switch statements
About Ruby hashes and symbols
Ruby C extension and volatile
Summarize Ruby and Dependency Injection
[Ruby] problem with if statement
About Ruby and object model
[Ruby] Singular methods and singular classes
About Ruby classes and instances
Ruby variables and functions (methods)
Ruby methods and classes (basic)
Creating Ruby classes and instances
[Ruby] Singular methods and singular classes
[Ruby] conditional branch if statement