Ruby standard control-flow (if, , ) प्रदान करता है लेकिन विशिष्ट, अभिव्यंजक विशेषताओं के साथ: (), , शक्तिशाली , और traditional loops की तुलना में की प्राथमिकता। Ruby में control flow स्वाभाविक रूप से पढ़ता है।
casewhiledo_x if conditionunlesscase/whenif age >= 18
puts "adult"
elsif age >= 13
puts "teen"
else
puts "child"
end
# MODIFIER form — condition at the END (reads like English, idiomatic for short cases)
puts "adult" if age >= 18
puts "minor" unless age >= 18 # `unless` = if not (more readable than `if !`)
# everything is an expression — if returns a value
grade = if score >= 90 then "A" else "B" end
Ruby का modifier form (puts "x" if condition) संक्षिप्त, पठनीय one-liners के लिए condition को अंत में रखता है। unless (= "if not") negation की तुलना में अधिक स्वाभाविक रूप से पढ़ता है। और if एक expression है जो एक value return करता है।
case status
when :active then "running"
when :stopped, :paused then "halted" # multiple values
when 1..10 then "small number" # ranges
when String then "it's a string" # types (uses ===)
else "unknown"
end
# case can also do structural pattern matching (Ruby 3)
case data
in { name: String => name, age: Integer } # pattern matching with destructuring
puts name
end
Ruby का case/when शक्तिशाली है — यह values, ranges, types, और (Ruby 3 में) structural patterns को match करता है, अंदरूनी तौर पर === का उपयोग करते हुए। एक basic switch की तुलना में कहीं अधिक लचीला।
while count < 10 do count += 1 end
until done do ... end # until = while not
# ✅ but Ruby PREFERS iteration with blocks over explicit loops:
5.times { |i| puts i } # instead of a for loop
[1, 2, 3].each { |n| puts n } # iterate a collection
(1..10).each { |n| ... } # iterate a range
1.upto(5) { |i| ... }
Ruby में while/until हैं, लेकिन idiomatic Ruby explicit loops की तुलना में blocks के साथ iteration को प्राथमिकता देता है (each, times, map) — अधिक अभिव्यंजक और Ruby जैसा।
Ruby के control flow को समझना मूलभूत रोज़मर्रा का ज्ञान है, और कई विशिष्ट विशेषताएँ Ruby के अभिव्यंजक, पठनीय दर्शन को दर्शाती हैं।
modifier form (do_x if condition) और unless (= "if not") आपको संक्षिप्त, English जैसी conditionals लिखने देते हैं जो idiomatic Ruby हैं और लगातार आती हैं।
यह तथ्य कि if एक expression है (एक value return करते हुए) elegant assignments को सक्षम बनाता है।
Ruby का case/when उल्लेखनीय रूप से शक्तिशाली है — === के माध्यम से values, ranges, types, और (Ruby 3 में) structural patterns को match करते हुए — इसे एक basic switch की तुलना में कहीं अधिक लचीला और साफ़ multi-way branching के लिए उपयोगी बनाता है।
निर्णायक रूप से, idiomatic Ruby traditional for/while loops की तुलना में blocks के साथ iteration को प्राथमिकता देता है (each, times, map, upto) — यह एक परिभाषित शैलीगत बिंदु है: Ruby developers explicit loops के बजाय block-based methods के साथ collections और ranges को iterate करते हैं, जो अधिक अभिव्यंजक और Ruby जैसा है।
इन constructs और idioms को समझना — पठनीय conditionals के लिए modifier form और unless, शक्तिशाली case/when, और विशेष रूप से explicit loops पर block-based iteration की प्राथमिकता — idiomatic Ruby लिखने और पढ़ने के लिए महत्वपूर्ण है।
चूँकि control flow हर program में आता है, और चूँकि Ruby के अभिव्यंजक forms (modifiers, unless, शक्तिशाली case, block iteration) इसके पठनीयता-केंद्रित दर्शन को मूर्त रूप देते हैं, उन पर महारत हासिल करना अन्य languages से loop-भारी patterns का अनुवाद करने के बजाय स्वाभाविक, idiomatic Ruby लिखने के लिए आवश्यक, मूलभूत ज्ञान है।