From: Peter Versteegen Date: 2007-08-22T20:21:37+09:00 Subject: Pretty Printing # Hi, # I need some help in simplefying my life # I need to create lots of tables from arrays and I need to provide # each of the columns with a heading. For eample, I have # two headings "a-value" and "b-value" specified as follows: a = ["a-value", "b-value"] b = [10, 10] # The array b contains the width of the field, so that I get: # a-value b-value # Two ways to do this are as follows: puts "#{'a-value'.rjust(10)}#{'b-value'.rjust(10)}" puts "#{a[0].rjust(b[0])}#{a[1].rjust(b[1])}" # which is difficult to debug, so that I'm looking for # a simpler process, specificaly by writing a function. # The following one works: def heading2(x, n) x.length.times do |i| print "#{x[i].rjust(n[i])}" end print "\n\n" end puts heading2(a,b) # But this approach doesn't. Why not? Are there other ways to make this simpler? def heading1(x, n) s = "" x.length.times do |i| s = s + "\#{\'" + x[i] + "\'.rjust(" + n[i].to_s + ")}" end p s print s puts s return s end heading1(a,b) puts "#{heading1(a, b)}" puts "\"#{heading1(a, b)}\"" # Thanks ahead. (NOTE: This file can be run from TextMate) Peter Versteegen pversteegen@gcnetmail.net