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

zip क्या है

zip दो lists से items को pair करता है जैसे एक zipper अपने teeth जोड़ता है। पहला पहले के साथ, दूसरा दूसरे के साथ, वगैरह।

In this page:

  1. zip
Syntax
ruby
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

markup
names = ["Alice", "Bob", "Cara"]
ages = [30, 25, 40]

paired = names.zip(ages)
puts paired.inspect

paired.each { |name, age| puts "#{name} is #{age}" }
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. अलग lengths के arrays zip करना और nil से surprised होना, जब छोटा array nil से pad होता है।
  2. यह उम्मीद करना कि zip एक hash return करेगा, जब यह pairs का एक array return करता है; एक hash बनाने के लिए .to_h इस्तेमाल करें।
  3. गलत तरीके से zip करना, इसलिए ages.zip(names) [age, name] pairs देता है।
🔒

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.