Ruby dynamically typed है — variables किसी type को declare नहीं करते और कोई भी value रख सकते हैं। Ruby में कई basic types हैं (सभी objects हैं), और variable scope दर्शाने के लिए यह naming conventions (sigils) का उपयोग करता है।
Variables और dynamic typing
count = 42 # no type declaration — inferred
name = "Ann"
count = "now a string" # dynamically typed — can change type
The basic types (all are objects)
42 # Integer
3.14 # Float
"hello" # String
:symbol # Symbol (an immutable, interned identifier)
true / false # Boolean (TrueClass / FalseClass)
nil # NilClass — "nothing" (Ruby's null)
[1, 2, 3] # Array
{ a: 1 } # Hash (key-value)
1..10 # Range
Variable naming conventions indicate scope
name = "local" # local variable (lowercase)
@instance = "instance" # @ → INSTANCE variable (per object)
@@class_var = "class" # @@ → CLASS variable (shared across instances)
$global = "global" # $ → global variable (avoid)
CONSTANT = "constant" # Uppercase start → CONSTANT (convention; can change with warning)
Ruby variable scope दर्शाने के लिए sigils (prefixes) का उपयोग करता है: सादे नाम local होते हैं, instance variables के लिए @, class variables के लिए @@, globals के लिए $ (हतोत्साहित), और constants के लिए uppercase। यह विशिष्ट है — prefix आपको एक नज़र में scope बता देता है।
Truthiness (a Ruby quirk)
# in Ruby, ONLY false and nil are falsy — EVERYTHING else is truthy
puts "truthy" if 0 # prints — 0 is TRUTHY in Ruby! (unlike many languages)
puts "truthy" if "" # prints — empty string is truthy too
puts "falsy" unless nil # nil is falsy
Ruby में केवल false और nil falsy हैं — 0, "", और [] सभी truthy हैं (कई languages के विपरीत जहाँ 0/empty falsy होते हैं)। यह एक आम भ्रम का स्रोत है।
यह क्यों महत्वपूर्ण है
Ruby के variables और basic types को समझना मूलभूत रोज़मर्रा का ज्ञान है।
Ruby dynamically typed है (variables कोई भी value रखते हैं, कोई type declaration नहीं), और एक विशिष्ट विशेषता है variable scope दर्शाने के लिए naming conventions (sigils) का इसका उपयोग — instance variables के लिए @, class variables के लिए @@, globals के लिए $, constants के लिए uppercase — ताकि variable का prefix आपको एक नज़र में इसका scope बता दे, जो Ruby को सही ढंग से पढ़ने और लिखने के लिए महत्वपूर्ण है (विशेष रूप से @instance variables, जो classes में लगातार उपयोग होते हैं)।
basic types को जानना (जो सभी Ruby के शुद्ध object-oriented model में objects हैं) — विशिष्ट Symbol type सहित — data के साथ काम करने के लिए आवश्यक है।
एक विशेष रूप से महत्वपूर्ण quirk जिसे समझना है वह है Ruby की truthiness: केवल false और nil falsy हैं, जबकि बाकी सब कुछ (0, "", और [] सहित) truthy है — यह कई languages से भिन्न है (जहाँ 0 या empty values falsy होते हैं) और अन्य languages से आने वाले developers के लिए bugs का एक आम स्रोत है।
dynamic typing, scope दर्शाने वाली naming conventions, basic types, और विशेष रूप से false-और-nil-केवल truthiness नियम को समझना सही Ruby लिखने और आम pitfalls से बचने के लिए मूलभूत है, जो इसे language के लिए आवश्यक, अवश्य-जानने योग्य ज्ञान बनाता है।
