Ruby मानक control-flow (if, , ) प्रदान करते पण विशिष्ट, अभिव्यक्तिमय वैशिष्ट्यांसह: (), , शक्तिशाली , आणि ब्लॉकच्या माध्यमातून ला परंपरागत 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) condition ला शेवटी ठेवतो संक्षिप्त, वाचनीय one-liners साठी. unless (= "if not") negation पेक्षा अधिक नैसर्गिकरित्या वाचला जातो. आणि if एक expression आहे जो एक मूल्य 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 शक्तिशाली आहे — हे मूल्यांची जुळवाजोळी करते, ranges, types, आणि (Ruby 3 मध्ये) structural patterns, === वापरून अंतर्गत. मूलभूत 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-like.
Ruby च्या control flow समजून घेणे हे मूलभूत रोजचे ज्ञान आहे, आणि अनेक विशिष्ट वैशिष्ट्य Ruby च्या अभिव्यक्तिमय, वाचनीय तत्वज्ञान प्रतिबिंबित करते.
Modifier form (do_x if condition) आणि unless (= "if not") तुम्हाला संक्षिप्त, English-like conditionals लिहायला देतात जे idiomatic Ruby आहेत आणि सर्वत्र दिसतात.
हे वस्तुस्थिती की if एक expression आहे (एक मूल्य return करतो) elegant assignments सक्षम करते.
Ruby चा case/when उल्लेखनीयरित्या शक्तिशाली आहे — मूल्यांची जुळवाजोळी करते, ranges, types, आणि (Ruby 3 मध्ये) === च्या माध्यमातून structural patterns — हे मूलभूत switch पेक्षा खूप अधिक लचकदार बनवते आणि clean multi-way branching साठी उपयोगी आहे.
गंभीरपणे, idiomatic Ruby traditional for/while loops पेक्षा blocks सह iteration ला प्राधान्य देते (each, times, map, upto) — हे एक परिभाषित शैलीबद्ध बिंदू आहे: Ruby developers collections आणि ranges iterate करतात block-based methods सह instead of explicit loops साठी, जे अधिक अभिव्यक्तिमय आणि Ruby-like आहे.
या constructs आणि idioms समजून घेणे — readable conditionals साठी modifier form आणि unless, शक्तिशाली case/when, आणि विशेषतः explicit loops पेक्षा block-based iteration ला प्राधान्य — idiomatic Ruby लिहायला आणि वाचायला महत्वाचे आहे.
कारण control flow प्रत्येक program मध्ये दिसते, आणि कारण Ruby चे अभिव्यक्तिमय forms (modifiers, unless, शक्तिशाली case, block iteration) त्याच्या readability-focused तत्वज्ञान मूर्त रूप देतात, त्यांमध्ये प्रवीणता मिळवणे आवश्यक, मूलभूत ज्ञान आहे natural, idiomatic Ruby लिहायला rather than other languages पासून loop-heavy patterns translate करण्यापेक्षा.
सविस्तर उत्तरांसह IT मुलाखत प्रश्नांचे ग्रंथालय — Junior पासून Senior पर्यंत.
देणगी द्या