Newbie: argh! array of array

Hi forum,

I’m fighting with a function like this! :slight_smile:

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 :slight_smile:

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

Alfonso C. wrote:

Hi forum,

I’m fighting with a function like this! :slight_smile:

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 :slight_smile:

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! :slight_smile:

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

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

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

It works fine, but I would like a “pseudo-code” like this :slight_smile:
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