7stud -- wrote:
> Ishan Nag wrote:
>> Thanks stud7 for the reply but what i was looking for was row column 
>> representation  of "Modules:" & "Classes" & "Exception classes"...i 
>> would be glad if you could help with that
> 
> How about:
> 
> modules = ["Modules", "A", "B", "C"]
> classes = ["Classes", "X", "Y", "Z"]
> exceptions = ["Exceptions", "E1", "E2", "E3"]
> 
> names = []
> names << modules << classes << exceptions
> results = names.transpose
> results.each do |arr|
>   arr.each do |name|
>     print name.ljust(20)
>   end
>   puts
> end
> 
> --output:--
> Modules             Classes             Exceptions
> A                   X                   E1
> B                   Y                   E2
> C                   Z                   E3

That doesn't work too good unless each heading has the same number of 
names.  Here is a more general solution:


modules = ["Modules", "A", "B", "C"]
classes = ["Classes", "X", "Y"]
exceptions = ["Exceptions", "E1", "E2", "E3", "E4"]

def my_transpose(*arrays)
  longest = arrays.max do |arr1, arr2|
    arr1.length <=> arr2.length
  end

  transposed = []
  longest.length.times do |i|
    row = []
    arrays.each do |arr|
      val = arr.at(i)
      if val
        row << val
      else
        row << ""
      end
    end

    transposed << row
  end

  transposed
end

results = my_transpose(modules, classes, exceptions)
p results

results.each do |arr|
  arr.each do |name|
    if name
      print name.ljust(20)
    else
      print " " * 20
    end
  end
  puts
end

--output:--
Modules             Classes             Exceptions
A                   X                   E1
B                   Y                   E2
C                                       E3
                                        E4

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