Output Manipulation

I

Ishan Nag

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
 
7

7stud --

Ishan said:
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 : "
 
I

Ishan Nag

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
 
7

7stud --

Ishan said:
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
 
7

7stud --

7stud said:
Ishan said:
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
 
7

7stud --

7stud said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top