Output Manipulation

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 P.ming 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

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 : "

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

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

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

thanks stud7…that was very helpful

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs