zip क्या है
zip दो lists से items को pair करता है जैसे एक zipper अपने teeth जोड़ता है। पहला पहले के साथ, दूसरा दूसरे के साथ, वगैरह।
In this page:
Syntax
array1.zip(array2)
zip
zip दो या ज़्यादा arrays को element-by-element छोटे arrays के एक array में combine करता है, position से corresponding elements pair करते हुए, एक zipper जैसा।
अगर arrays अलग lengths की हों, छोटे वालों से missing elements nil बन जाते हैं। यह अक्सर दो related arrays को साथ iterate करने के लिए इस्तेमाल होता है।
Note:
array1.zip(array2).each { |a, b| ... } lockstep में दो arrays साथ iterate करने का एक common idiom है।उदाहरण: zip
names = ["Alice", "Bob", "Cara"]
ages = [30, 25, 40]
paired = names.zip(ages)
puts paired.inspect
paired.each { |name, age| puts "#{name} is #{age}" }
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. #}
आम गलतियां
- अलग lengths के arrays zip करना और
nilसे surprised होना, जब छोटा arraynilसे pad होता है। - यह उम्मीद करना कि
zipएक hash return करेगा, जब यह pairs का एक array return करता है; एक hash बनाने के लिए.to_hइस्तेमाल करें। - गलत तरीके से zip करना, इसलिए
ages.zip(names)[age, name]pairs देता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: