← Back to Ruby Course | Chapter 2: Variables & Types | Lesson 5 of 7

nil क्या है

nil Ruby का 'यहाँ कुछ नहीं' कहने का तरीका है, एक empty box की तरह। यह zero से अलग है, जो एक number है, क्योंकि nil मतलब बिल्कुल कोई value नहीं है।

In this page:

  1. nil
Syntax
ruby
variable = nil
variable.nil?

nil

nil Ruby में एक value की absence represent करता है, दूसरी languages में null जैसा, और यह NilClass का एक instance है।

Uninitialized instance variables default से nil होते हैं, और कई methods "कुछ नहीं मिला" signal करने के लिए nil return करते हैं। nil.to_s एक empty string देता है और nil.to_a एक empty array देता है, जो कुछ contexts में convenient हो सकता है।

Warning: nil पर ज़्यादातर methods call करना (NilClass खुद define करने वाले methods के अलावा) एक NoMethodError raise करता है -- इस्तेमाल से पहले हमेशा nil check करें अगर यह possible हो।

उदाहरण: nil

markup
value = nil

puts value.nil?
puts value.class
puts value.to_s.empty?

value = "now set"
puts value.nil?
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. nil पर एक method call करना, जैसे value.upcase, जो NoMethodError raise करता है।
  2. यह मान लेना कि nil और एक empty string same हैं, जब nil.to_s "" है लेकिन nil, "" नहीं है।
  3. value == nil से ऐसे तरीकों से test करना जो safe navigation value&.upcase miss करें।
🔒

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.