al_S
December 24, 2009, 12:32am
1
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
al_S
December 24, 2009, 3:27pm
2
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
al_S
December 25, 2009, 5:46pm
3
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
al_S
December 25, 2009, 6:04pm
4
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