unless क्या है
unless if का opposite है: यह code सिर्फ तभी चलाता है जब condition false हो। यह 'unless it is raining, we go outside' कहने जैसा है।
In this page:
Syntax
unless condition
# runs when condition is false
else
# otherwise
end
unless
unless condition इसका block सिर्फ तभी चलाता है जब condition falsy हो, if के inverse की तरह act करते हुए। यह तब इस्तेमाल होता है जब negated phrasing if !condition से ज़्यादा naturally पढ़ी जाए।
unless का भी एक trailing modifier form है, जैसे puts "error" unless valid।
Note:
unless आमतौर पर सिर्फ simple conditions के साथ इस्तेमाल होना चाहिए -- इसे else के साथ pair करने से बचें, जो अक्सर पढ़ने में confusing होता है।उदाहरण: 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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
unlessमें एकelseयाandके साथ complex conditions add करना, जो पढ़ना मुश्किल बना देता है; इसकी बजायifइस्तेमाल करें।unlessको एक negation के साथ इस्तेमाल करना जैसेunless !logged_in, एक confusing double negative बनाते हुए।unlessके साथelsifइस्तेमाल करना, जो allowed नहीं है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: