Array Methods का परिचय
Arrays दर्जनों handy built-in methods के साथ आते हैं, lists के लिए एक Swiss army knife की तरह। आप बिना steps खुद लिखे count, sort, reverse, और चीज़ें ढूंढ सकते हैं।
In this page:
Syntax
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
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
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
sortcall करना औरnumbersबदलने की उम्मीद करना, जब यह एक नया array return करता है जब तक आपsort!इस्तेमाल न करें।- numbers और strings जैसे mixed types वाला एक array sort करना, जो
ArgumentErrorraise करता है। - एक गलत type के साथ
include?इस्तेमाल करना, जैसेnumbers.include?("8"), जोfalseहै।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: