← Back to Ruby Course | Chapter 6: Control Flow | Lesson 2 of 7

unless क्या है

unless if का opposite है: यह code सिर्फ तभी चलाता है जब condition false हो। यह 'unless it is raining, we go outside' कहने जैसा है।

In this page:

  1. unless
Syntax
ruby
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

markup
logged_in = false

unless logged_in
  puts "Please log in"
end

puts "Access denied" unless logged_in
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. #}
आम गलतियां
  1. unless में एक else या and के साथ complex conditions add करना, जो पढ़ना मुश्किल बना देता है; इसकी बजाय if इस्तेमाल करें।
  2. unless को एक negation के साथ इस्तेमाल करना जैसे unless !logged_in, एक confusing double negative बनाते हुए।
  3. unless के साथ elsif इस्तेमाल करना, जो allowed नहीं है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.