Introduction
Boolean values help us decide if something is true or false. nil means nothing or no value.
Boolean values help us decide if something is true or false. nil means nothing or no value.
true false nil
true and false are Boolean values representing yes/no or on/off.
nil means no value or nothing, similar to null in other languages.
is_raining = true is_sunny = false no_value = nil
if is_raining puts "Take an umbrella" else puts "No umbrella needed" end
value = nil if value.nil? puts "No value found" end
This program decides what to do based on hunger and food availability using Boolean values and nil.
is_hungry = true has_food = false meal = nil if is_hungry && has_food meal = "Eating now" elsif is_hungry && !has_food meal = "Need to buy food" else meal = "Not hungry" end puts meal puts "Meal variable is nil? #{meal.nil?}"
In Ruby, only false and nil are treated as false in conditions; everything else is true.
Use .nil? method to check if a variable is nil.
true and false represent yes/no or on/off.
nil means no value or nothing.
Use Boolean values to make decisions in your code.
nilnil is used to represent the absence of any value, like an empty box.true and false are Boolean values meaning yes or no, but not "no value". 0 is a number, not a special value for nothing.nil -> Option Anil means no value [OK]true and false for Boolean values.False and FALSE are not valid Ruby keywords. FalseValue is not a keyword but a name.value = nil if value puts "Yes" else puts "No" end
nil in conditionsnil is treated as false in conditions.value is nil, the if value condition is false, so the else branch runs, printing "No".nil acts as false in if [OK]if false puts "No" elsif nil puts "Maybe" else puts "Yes" end
if, elsif, and end, so no syntax error.false is false, nil is also false, so both conditions fail and else runs, printing "Yes".status is either true or nil in Ruby. Which condition correctly does this?status is either true OR nil, so use logical OR (||).status == true || status == nil, which matches the requirement. if status == true && status == nil uses AND, which can never be true for both at once. if status || nil is incorrect syntax and logic. if status != false checks if status is not false, which includes other values too.Pass the test and this pattern moves unknown