0

Coming from other languages where for loop is predominant way of iterating, I am curious is there a better more Ruby style way to achieve below:

q=[5,1,7,9,0,3,6,8,0]

for i in 0..q.size/3-1

  do one thing with q[0+i*3]

  do another one with q[1+i*3]

  third operation on q[2+i*3]

end

THX!

Code submitted in question actually works, but knowing that for in Ruby uses each under the hood, I am not sure what is the best way to make it more compact&efficient.

2 Answers 2

4

The most Ruby way to do this would probably be to use Enumerable#each_slice:

q.each_slice(3) do |first, second, third|
  # do something with `first`
  # do another thing with `second`
  # third operation with `third`
end

For example:

q.each_slice(3) do |first, second, third|
  p({ first:, second:, third: })
end
# { first: 5, second: 1, third: 7 }
# { first: 9, second: 0, third: 3 }
# { first: 6, second: 8, third: 0 }

In general, one of the most important operations in any program is "iterating over stuff" (or more generally, traversing, transforming, filtering, building, and folding collections), and in Ruby, that is handled by the Enumerable mixin. Studying and understanding this mixin's documentation is crucial to writing good, idiomatic Ruby code.

As a (slightly exaggerated) general rule, if you have to write a loop or manually fiddle with indices or counters in Ruby, you are very likely doing something wrong. In fact, with the powerful algorithms, data structures, and collections libraries that are shipped with most modern programming languages, that does not just apply to Ruby but almost universally.

3
  • ZoRaN, notice that enum1 = q.each_slice(3) #=> #<Enumerator: [5, 1, 7, 9, 0, 3, 6, 8, 0]:each_slice(3)>. We can see the objects this enumerator will generate by converting it to an array: Enum1.to_a #=> [[5, 1, 7], [9, 0, 3], [6, 8, 0]]. But wait, each_slice is an Enumerable method, but q, being an array, only responds to Array methods. So how does that work?... May 21 at 22:00
  • ...For a class to be able to use methods defined in the module Enumerable, the class (Array being one) must satisfy two requirements: it must Module#include the module (e.g., Array.include(Enumerable)) and define a method each on the class that returns an enumerator (i.e., an instance of the class Enumerator). That's because Enumerable methods can only be invoked on objects that are elements of the class Enumerator... May 21 at 22:00
  • ...So there is actually a step that you are not seeing: enum0 = q.each #=> #<Enumerator: [5, 1, 7, 9, 0, 3, 6, 8, 0]:each> (note: enum0.to_a #=> [5, 1, 7, 9, 0, 3, 6, 8, 0]), then enum1 = enum0.each_slice(3) #=> #<Enumerator: #<Enumerator: [5, 1, 7, 9, 0, 3, 6, 8, 0]:each>:each_slice(3)> . Many built-in classes include Enumerable and define each on the class, (Hash and Range being two others). You can include Enumerable methods in custom classes in the same way. Now, enum1.map { |arr| arr.sum } #=> [13, 12, 14]. Why? May 21 at 22:00
0

As already pointed out, each_slice is pretty much the ideal answer here.

However if you were purely interested in looping over the numeric indices, transforming the code you have into something more idiomatically Ruby, you might use a range and step to move forwards by in increments of 3:

q=[5,1,7,9,0,3,6,8,0]

(0..q.size - 1).step(3) do |i|
  a = q[i]
  b = q[i + 1]
  c = q[i + 2]

  p [a, b, c]
end

# [5, 1, 7]
# [9, 0, 3]
# [6, 8, 0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.