Syntax
first.zip(second)Examples
Pairing Sequences with zip
Run as a Ruby 3.2+ script. Required libraries are named in the example; declare bundled/default gems in your Gemfile when using Bundler.
names = %w[Ada Lin Sam]
scores = [9, 7]
p names.zip(scores)
names.zip(scores).each do |name, score|
puts "#{name}: #{score || "pending"}"
end- Expected output: [["Ada", 9], ["Lin", 7], ["Sam", nil]] Ada: 9 Lin: 7 Sam: pending
- The third score is absent, so zip inserts nil. Zero would still display as zero because Ruby treats it as truthy.
Best practices
- Validate equal lengths when missing partners indicate corrupt input.
- Use keyed records instead of parallel arrays when the data can be represented that way.
Runtime & versions
Ruby interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Ruby documentation