If the numerical value num is in the range of 1 or more and 10 or less, True is output. Or, if outside_mode is True, True is output even if the numerical value num is 0 or less and 11 or more.
This process will be described using conditional branching.
Output example:
in1to10(5, false)
=>True
in1to10(11, false)
=>False
in1to10(11 true)
=>True
This time we will use logical operators.
#true if both a and b are true
a && b
#true if either a or b is true
a || b
Ruby's logical operators evaluate conditional expressions from the left side to the right side, and if the evaluation of the entire expression is confirmed, it does not evaluate at that point.
def in1to10 (num, outside_mode)
if (num >= 1 && num <= 10) || outside_mode
puts "True"
else
puts "False"
end
end
By making such a description, it is possible to output as in the output example.
Recommended Posts