Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 738069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:47:39+00:00 2026-05-14T07:47:39+00:00

What’s a better way to traverse an array while iterating through another array? For

  • 0

What’s a better way to traverse an array while iterating through another array? For example, if I have two arrays like the following:

names = [ "Rover", "Fido", "Lassie", "Calypso"]
breeds = [ "Terrier", "Lhasa Apso", "Collie", "Bulldog"]

Assuming the arrays correspond with one another – that is, Rover is a Terrier, Fido is a Lhasa Apso, etc. – I’d like to create a dog class, and a new dog object for each item:

class Dog
  attr_reader :name, :breed

  def initialize(name, breed)
    @name = name
    @breed = breed
  end
end

I can iterate through names and breeds with the following:

index = 0

names.each do |name|
  Dog.new("#{name}", "#{breeds[index]}")
  index = index.next
end

However, I get the feeling that using the index variable is the wrong way to go about it. What would be a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:47:40+00:00Added an answer on May 14, 2026 at 7:47 am
    dogs = names.zip(breeds).map { |name, breed| Dog.new(name, breed) }
    

    Array#zip interleaves the target array with elements of the arguments, so

    irb> [1, 2, 3].zip(['a', 'b', 'c'])
     #=> [ [1, 'a'], [2, 'b'], [3, 'c'] ]
    

    You can use arrays of different lengths (in which case the target array determines the length of the resulting array, with the extra entries filled in with nil).

    irb> [1, 2, 3, 4, 5].zip(['a', 'b', 'c'])
     #=> [ [1, 'a'], [2, 'b'], [3, 'c'], [4, nil], [5, nil] ]
    irb> [1, 2, 3].zip(['a', 'b', 'c', 'd', 'e'])
     #=> [ [1, 'a'], [2, 'b'], [3, 'c'] ]
    

    You can also zip more than two arrays together:

    irb> [1,2,3].zip(['a', 'b', 'c'], [:alpha, :beta, :gamma])
     #=> [ [1, 'a', :alpha], [2, 'b', :beta], [3, 'c', :gamma] ]
    

    Array#map is a great way to transform an array, since it returns an array where each entry is the result of running the block on the corresponding entry in the target array.

    irb> [1,2,3].map { |n| 10 - n }
     #=> [ 9, 8, 7 ]
    

    When using iterators over arrays of arrays, if you give a multiple parameter block, the array entries will be automatically broken into those parameters:

    irb> [ [1, 'a'], [2, 'b'], [3, 'c'] ].each { |array| p array }
    [ 1, 'a' ]
    [ 2, 'b' ]
    [ 3, 'c' ]
    #=> nil
    irb> [ [1, 'a'], [2, 'b'], [3, 'c'] ].each do |num, char| 
    ...>   puts "number: #{num}, character: #{char}" 
    ...> end
    number 1, character: a
    number 2, character: b
    number 3, character: c
    #=> [ [1, 'a'], [2, 'b'], [3, 'c'] ]
    

    Like Matt Briggs mentioned, #each_with_index is another good tool to know about. It iterates through the elements of an array, passing a block each element in turn.

    irb> ['a', 'b', 'c'].each_with_index do |char, index| 
    ...>   puts "character #{char} at index #{index}"
    ...> end
    character a at index 0
    character b at index 1
    character c at index 2
    #=> [ 'a', 'b', 'c' ]
    

    When using an iterator like #each_with_index you can use parentheses to break up array elements into their constituent parts:

    irb> [ [1, 'a'], [2, 'b'], [3, 'c'] ].each_with_index do |(num, char), index| 
    ...>   puts "number: #{num}, character: #{char} at index #{index}" 
    ...> end
    number 1, character: a at index 0
    number 2, character: b at index 1
    number 3, character: c at index 2
    #=> [ [1, 'a'], [2, 'b'], [3, 'c'] ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Any version of Data::Alias built before Version 1.08 (Released October… May 14, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer By far the easiest way is to refresh the ArrayCollection… May 14, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer The problem is that you don't actually want to delete… May 14, 2026 at 6:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.