Frank
Frank

Reputation: 33

Is it possible to make this loop simpler?

The duplication of p1, p2, p3 bugs me.

y.zip(r, q).each { |p1, p2, p3|
  puts '%5s %5s | %3s' % [p1, p2, p3]
}

Maybe there is a solution with map? Is there a way to insert the variables directly into the string rather than using this formatting?

Upvotes: 0

Views: 51

Answers (2)

SteveTurczyn
SteveTurczyn

Reputation: 36860

Each element of the zip'd array is an array, you can just use the whole array

y.zip(r, q).each {|p| puts '%5s %5s | %3s' % p}

Upvotes: 5

Aetherus
Aetherus

Reputation: 8888

y.zip(r, q).each {|*p| puts '%5s %5s | %3s' % p}

Upvotes: 0

Related Questions