unless
In this page:
unless
unless condition runs its block only when the condition is falsy, acting as the inverse of if. It's used when the negated phrasing reads more naturally than if !condition. unless also has a trailing modifier form, like puts "error" unless valid.
Note: unless should generally only be used with simple conditions -- avoid pairing it with else, which is often confusing to read.
Example: unless
logged_in = false
unless logged_in
puts "Please log in"
end
puts "Access denied" unless logged_in
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: