I have A, an array of arrays of length 3. For example A = [[a1, b1, c3], [a2, b2, c2], [a3, b3, c3], [a4, b4, c4]]. Right now I am looping over it like this:
A.each do |elem|
puts(elem[0].foo)
puts(elem[1].bar)
puts(elem[2].baz)
end
Since I am using a lot of different properties in the loop, the code gets pretty messy and unreadable. Plus, the local name elem[0] isn't very descriptive. Is there a way to use something like this?
A.each do |[a,b,c]|
puts(a.foo)
puts(b.bar)
puts(c.baz)
end
I'm pretty new to ruby I don't really know where to look for something like this.
do |a,b,c|instead ofdo |[a,b,c]||a,b,c|.aandbare the imperative, one can use array decomposition with the splat (*) operator as well such that|a,b,*c|(it is customary to use*restin place of*c). In this caseaandbwill be the first and second elements of theArrayand*cwill contain 0 or more elements that identify the rest of theArray.