← Back to Ruby Course | Chapter 4: Arrays | Lesson 6 of 7

Array Methods का परिचय

Arrays दर्जनों handy built-in methods के साथ आते हैं, lists के लिए एक Swiss army knife की तरह। आप बिना steps खुद लिखे count, sort, reverse, और चीज़ें ढूंढ सकते हैं।

In this page:

  1. Array Methods
Syntax
ruby
array.sort
array.reverse
array.uniq
array.include?(value)
array.first
array.last

Array Methods

Ruby arrays में basics से आगे दर्जनों useful built-in methods हैं, जैसे .sort, .uniq, .include?, .sum, .min, .max, और .first/.last।

इनमें से ज़्यादातर Enumerable module से आते हैं जिसे Array include करता है, इसलिए ये Hash और Range जैसे दूसरे collection types पर भी available हैं। इन built-ins को सीखना manual loops की बजाय concise, idiomatic Ruby लिखने की key है।

Note: क्योंकि Array, Enumerable include करता है, sum, min, और sort जैसे methods किसी भी custom class पर भी काम करते हैं जो Enumerable include करती है।

उदाहरण: Array Methods

markup
numbers = [5, 3, 8, 1, 9, 3]

puts numbers.sort.inspect
puts numbers.uniq.inspect
puts numbers.include?(8)
puts numbers.sum
puts numbers.max
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. sort call करना और numbers बदलने की उम्मीद करना, जब यह एक नया array return करता है जब तक आप sort! इस्तेमाल न करें।
  2. numbers और strings जैसे mixed types वाला एक array sort करना, जो ArgumentError raise करता है।
  3. एक गलत type के साथ include? इस्तेमाल करना, जैसे numbers.include?("8"), जो false है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

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