0

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?

2
  • 1
    what is the value of y? Commented Dec 31, 2015 at 0:48
  • y, r and q are all arrays with equal length Commented Dec 31, 2015 at 1:10

2 Answers 2

5

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}
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

That's not going to work, p will be an array containing a nested array and the nested array will have three elements but the outer array won't. You'll get a "too few arguments" for the puts.
What is your '*p' supposed to do? It gives me an ArgumentError for using too few arguments.
*p is a way of packing (or unpacking) arguments into (out of) an array. It can be a useful technique.... for example if you have a method foo and you don't know how many arguments will be passed, you can automatically combine them into an array. You would do foo('Sam', 'Joe', 'Pete') but when you define the method you woul do def foo(*names) giving you an array called names instead of def foo(name_1 name_2, name_3) giving you three separate variables.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.