Output Manipulation

Discussion in 'Ruby' started by Ishan Nag, Aug 21, 2009.

  1. Ishan Nag

    Ishan Nag Guest

    puts "\nMODULE : "
    Module.constants.sort.select { |x| eval(x.to_s).instance_of? Module }
    puts"\nCLASSES : "

    puts Module.constants.sort.select { |x|
    c = eval(x.to_s)
    c.is_a? Class and not c.ancestors.include? Exception
    }
    puts " \nEXCEPTIONS :"
    puts Module.constants.sort.select {|x|
    c = eval(x.to_s)
    c.instance_of? Class and c.ancestors.include? Exception
    }



    Above code is taken from 'The Ruby Programming Language'; it prints all
    the modules , clases and exception classes when the interpreter is
    loaded ; however i want the output in row column form...please help
    --
    Posted via http://www.ruby-forum.com/.
    Ishan Nag, Aug 21, 2009
    #1
    1. Advertising

  2. Ishan Nag

    7stud -- Guest

    Ishan Nag wrote:
    > puts "\nMODULE : "
    > Module.constants.sort.select { |x| eval(x.to_s).instance_of? Module }
    > puts"\nCLASSES : "
    > however i want the output in row column form...please help


    print "\nMODULE : "
    print Module.constants.sort.select { |x| eval(x.to_s).instance_of?
    Module }[1, 20].join(", ")
    puts"\nCLASSES : "

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Aug 21, 2009
    #2
    1. Advertising

  3. Ishan Nag

    Ishan Nag Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    Ishan Nag, Aug 21, 2009
    #3
  4. Ishan Nag

    7stud -- Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Aug 21, 2009
    #4
  5. Ishan Nag

    7stud -- Guest

    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/.
    7stud --, Aug 22, 2009
    #5
  6. Ishan Nag

    7stud -- Guest

    7stud -- wrote:
    >
    > results.each do |arr|
    > arr.each do |name|
    > if name
    > print name.ljust(20)
    > else
    > print " " * 20
    > end
    > end
    > puts
    > end
    >


    That section can now be reduced to:

    results.each do |arr|
    arr.each do |name|
    print name.ljust(20)
    end
    puts
    end

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Aug 22, 2009
    #6
  7. Ishan Nag

    Ishan Nag Guest

    Ishan Nag, Aug 24, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    584
    John Bentley
    Sep 10, 2003
  2. chuck amadi
    Replies:
    1
    Views:
    479
    Larry Bates
    Jun 23, 2004
  3. Paul
    Replies:
    1
    Views:
    425
  4. jmr
    Replies:
    2
    Views:
    325
  5. Logan Lee
    Replies:
    15
    Views:
    539
    Barry Schwarz
    Dec 19, 2007
Loading...

Share This Page