← Back to Ruby Course | Chapter 12: Ruby Best Practices | Lesson 1 of 6

Idiomatic Ruby कैसे लिखें

Idiomatic Ruby का मतलब है जिस तरीके से Ruby folks पसंद करते हैं उस तरह लिखना, clear names और छोटी, expressive lines के साथ। यह locals की तरह एक language बोलने जैसा है।

In this page:

  1. Idiomatic Ruby

Idiomatic Ruby

Idiomatic Ruby expressive, readable code favor करता है: snake_case method और variable names, small focused methods, manual loops की बजाय existing Enumerable methods इस्तेमाल करना, और branches के अंदर assign करने की बजाय if/case को सीधे values return करने देना।

Ruby community code को लगभग natural language की तरह पढ़ने पर strong emphasis देती है। established style conventions follow करना (जैसे community-maintained Ruby Style Guide) code को दूसरे Ruby developers के लिए ज़्यादा approachable बनाता है।

Note: if-check और array-building वाले एक manual loop से numbers.select(&:even?) prefer करें -- यह intent के ज़्यादा करीब पढ़ा जाता है।

उदाहरण: Idiomatic Ruby

markup
numbers = [1, 2, 3, 4, 5, 6]

# Idiomatic: uses built-in Enumerable methods
evens = numbers.select(&:even?)
puts evens.inspect

# Idiomatic: if as an expression
label = if numbers.length > 5 then "long" else "short" end
puts label
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. camelCase में def getName लिखना, जब Ruby get_name जैसे snake_case इस्तेमाल करता है।
  2. एक index वाला एक for loop लिखना जब each या map clearer है।
  3. last line पर एक explicit return add करना, जब last expression automatically return होता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.