# Newbie: argh! array of array

**URL:** <https://www.ruby-forum.com/t/newbie-argh-array-of-array/180628>\
**Category:** Ruby\
**Created:** [December 24, 2009, 12:32am UTC](https://www.ruby-forum.com/t/newbie-argh-array-of-array/180628 "2009-12-24T00:32:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![al\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@al\_S](https://www.ruby-forum.com/u/al_S)\
**Post date:** [December 24, 2009, 12:32am UTC](https://www.ruby-forum.com/t/newbie-argh-array-of-array/180628/1 "2009-12-24T00:32:27Z")

</div>

Hi forum,

I’m fighting with a function like this! 🙂

function([“x”,“y”,“x”]) do |a,b|c|  
printf("%10s %10s %10s\n",a,b,c)  
end

It works fine, but I would like a “pseudo-code” like this 🙂

new array

function([“x”,“y”,“z”]) do |a,b|c|  
push a,b,c into array  
end

if array is not empty  
printf("%10s %10s %10s\n",a,b,c)  
end

Which is the best way to write it?

Thank you very very much,  
Al

---

<div class="post-metadata">

**Author:** ![al\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@al\_S](https://www.ruby-forum.com/u/al_S)\
**Post date:** [December 24, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/newbie-argh-array-of-array/180628/2 "2009-12-24T15:27:45Z")

</div>

Alfonso C. wrote:

> Hi forum,
> 
> I’m fighting with a function like this! 🙂
> 
> function([“x”,“y”,“x”]) do |a,b|c|  
> printf("%10s %10s %10s\n",a,b,c)  
> end
> 
> It works fine, but I would like a “pseudo-code” like this 🙂
> 
> new array
> 
> function([“x”,“y”,“z”]) do |a,b|c|  
> push a,b,c into array  
> end
> 
> if array is not empty  
> printf("%10s %10s %10s\n",a,b,c)  
> end
> 
> Which is the best way to write it?
> 
> Thank you very very much,  
> Al

Check it out! 🙂

array = []

function([“x”,“y”]) do |a,b|  
array.push([["#{a.value}"],["#{b.value}"]])  
end

if not array.empty?  
array.each {|z,k| printf("%10s %s\n",z,k)}  
end

---

<div class="post-metadata">

**Author:** ![al\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@al\_S](https://www.ruby-forum.com/u/al_S)\
**Post date:** [December 25, 2009, 5:46pm UTC](https://www.ruby-forum.com/t/newbie-argh-array-of-array/180628/3 "2009-12-25T17:46:37Z")

</div>

Hi,

Am Donnerstag, 24. Dez 2009, 08:32:29 +0900 schrieb Alfonso C.:

> meth(sth) do |a,b,c|  
> push a,b,c into array  
> end
> 
> if array is not empty  
> printf("%10s %10s %10s\n",a,b,c)  
> end

You maybe mean a situation in that the length of the argument list  
can change: |a,b,c,…|. Then you could use splats. Here’s an  
example:

def fade\_away  
yield “a”, “b”, “c”  
yield “a”, “b”  
yield “a”  
yield  
end

fade\_away do |\*args|  
if args.any? then  
puts args.join  
else  
puts “.”  
end  
end

You can use splats in many other contexts:

def some\_method \*args  
a, b, c, \* = \*args  
end

ary = [/^y/, “ja”, “oui”, “si”, “sí”]  
case answ  
when \*ary then do\_it  
end

Everything untested.

Bertram

---

<div class="post-metadata">

**Author:** ![al\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f08c70/32.png) [@al\_S](https://www.ruby-forum.com/u/al_S)\
**Post date:** [December 25, 2009, 6:04pm UTC](https://www.ruby-forum.com/t/newbie-argh-array-of-array/180628/4 "2009-12-25T18:04:48Z")

</div>

On 12/24/2009 12:32 AM, Alfonso C. wrote:

> It works fine, but I would like a “pseudo-code” like this 🙂  
> end
> 
> Which is the best way to write it?

I am having difficulties to figure what you want. Do you want to print  
arbitrary long arrays of strings with a particular formatting? If so,  
you can do any of these

puts arr.map {|x| ‘%10s’ % x}.join(’ ')

printf arr.map{’%10s’}.join(’ ') \<\< “\n”, \*arr

Kind regards

robert
