1

I have two arrays:

a = [1, 2 ,3]
b = [4, 5, 5]

I want to iterate over all elements of both arrays. One way of doing so would be the following:

(a + b).each do |element|
  puts element
end

This creates a third array from a and b and then iterates over it. Are there more elegant/Rubyist methods to do the same?

onebree
  • 1,853
  • 1
  • 17
  • 44
Martin T.
  • 63
  • 5
  • 4
    What would qualify as "more eloquent"? – Dave Newton Sep 09 '14 at 20:59
  • For example without crating a new array? – Martin T. Sep 09 '14 at 21:01
  • One could write the loops twice, each for one list, but this truly violates DRY – Martin T. Sep 09 '14 at 21:03
  • http://stackoverflow.com/questions/3580049/whats-the-ruby-way-to-iterate-over-two-arrays-at-once http://stackoverflow.com/questions/19062972/ruby-code-to-iterate-through-two-array-simulataneoulsy http://stackoverflow.com/questions/4816959/best-way-to-iterate-over-multiple-arrays – dee-see Sep 09 '14 at 21:03
  • **not** simulataneoulsy, one after another! – Martin T. Sep 09 '14 at 21:04
  • ok I wasn't sure. That's why I didn't vote to close! – dee-see Sep 09 '14 at 21:05
  • your choices really are to create a third `Array` or iterate each independently. There are lost of ways to create this third `Array` and many `DRY` ways to iterate each independently but from a conciseness standpoint you have already succeeded so i don't understand the question. – engineersmnky Sep 09 '14 at 21:11
  • One simple option would refactor out the content of the block to a dedicated function and call it in both blocks - other ideas? The question is more are other possibilities to archive a goal! – Martin T. Sep 09 '14 at 21:17
  • 1
    maybe with ruby 2.0 `lazy`? `[listA, listB].lazy.flatten.each` .... I do not know `lazy` but from what I read it sounded as if it would be suitable. – Felix Sep 09 '14 at 21:21
  • If you want truly _simultaneous_ looping, you'l want to use multithreading using Thread or a higher-level library like Celluloid. Then you can run each loop in its own thread, in parallel. It's a lot more complexity, only justified if you are frequently iterating large loops. – mahemoff Sep 09 '14 at 21:34
  • I'm almost certain there's no way to avoid creating a third array. Any particular reason why this is an issue? – fny Sep 09 '14 at 22:27
  • OP's example may be simply rewritten as `puts a, b` ;) – David Unric Sep 10 '14 at 03:45

1 Answers1

1

If you merely want to print each object from both arrays:

puts a, b

If you are looking for a solution to a more general scenario, there is no way to avoid creating a third array, but this third array goes away unless assigned to an object. (If you called third_arr = a + b.)

If performance is an issue, you can avoid iterating through a large array. Say that lengths of a and b are 100 each, meaning the "third array" will have 200 elements. You can split the work up by each individual array, while keeping the code DRY.

[a, b].each do |arr|
  arr.each do |item|
    # ...
  end
end
onebree
  • 1,853
  • 1
  • 17
  • 44