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

ยทยทยท

--
Posted via http://www.ruby-forum.com/.

Hi,

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

ยทยทยท

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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step&gt;\.

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

ยทยทยท

On 12/24/2009 12:32 AM, Alfonso Caponi 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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Alfonso Caponi 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

ยทยทยท

--
Posted via http://www.ruby-forum.com/\.