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 کو آخر میں رکھتی ہے تاکہ مختصر اور قابلِ فہم single-liners لکھے جا سکیں۔ unless (= "if not") منطقی طور پر نفی سے زیادہ قدرتی پڑھا جاتا ہے۔ اور if ایک expression ہے جو کوئی قدر واپس کرتا ہے۔
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 کرتا ہے، === کو کام میں لاتے ہوئے۔ بنیادی 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 blocks کے ساتھ iteration (each, times, map) کو واضح loops کے بجائے ترجیح دیتا ہے — زیادہ بیان کرنے میں آسان اور Ruby جیسا۔
Ruby کے control flow کو سمجھنا روزمرہ کا بنیادی علم ہے، اور کئی منفرد خصوصیات Ruby کے بیان میں آسان اور پڑھنے میں آسان فلسفہ کو ظاہر کرتی ہیں۔
Modifier form (do_x if condition) اور unless (= "if not") آپ کو مختصر، انگریزی جیسی conditionals لکھنے دیتے ہیں جو idiomatic Ruby ہیں اور ہمیشہ نظر آتی ہیں۔
حقیقت یہ ہے کہ if ایک expression ہے (جو قدر واپس کرتا ہے) elegant assignments کو ممکن بناتا ہے۔
Ruby کی case/when قابلِ غور طور پر طاقتور ہے — values، ranges، types، اور (Ruby 3 میں) structural patterns کو === کے ذریعے match کرتا ہے — اسے بنیادی switch سے بہت زیادہ لچکدار بناتا ہے اور صاف multi-way branching کے لیے مفید۔
اہم بات یہ ہے کہ idiomatic Ruby blocks کے ساتھ iteration (each, times, map, upto) کو روایتی for/while loops کے بجائے ترجیح دیتا ہے — یہ ایک حد تک اہم stylistic نقطہ ہے: Ruby developers collections اور ranges کو block-based methods کے ساتھ iterate کرتے ہیں نہ کہ واضح loops کے ساتھ، جو زیادہ بیان کرنے میں آسان اور Ruby جیسا ہے۔
ان constructs اور idioms کو سمجھنا — readable conditionals کے لیے modifier form اور unless، طاقتور case/when، اور خاص طور پر explicit loops کے بجائے block-based iteration کی ترجیح — idiomatic Ruby لکھنے اور پڑھنے کے لیے اہم ہے۔
چونکہ control flow ہر پروگرام میں ظاہر ہوتا ہے، اور چونکہ Ruby کی بیان میں آسان forms (modifiers، unless، طاقتور case، block iteration) اس کی readability-focused فلسفہ کا مظہر ہیں، انہیں ہضم کرنا قدرتی، idiomatic Ruby لکھنے کے لیے بنیادی، اہم علم ہے نہ کہ دوسری زبانوں سے loop-heavy patterns کو ترجمہ کرنا۔