formatting print output

J

joemacbusiness

Hi All,

How do I format the output from print or puts
so that columns are aligned:

So this:
beagle-status 255
bibtex 154312
bioradtopgm 7844
bison 242032
blackjack 140792


becomes this:

beagle-status 255
bibtex 154312
bioradtopgm 7844
bison 242032
blackjack 140792

You probably know what I mean.
I could not find this in my Ruby book (Cooper)
and a lot of the Ruby Websites cannot be accessed.

Thank you, --JM
 
E

Eustáquio Rangel

How do I format the output from print or puts
so that columns are aligned:

So this:
beagle-status 255
bibtex 154312
bioradtopgm 7844
bison 242032
blackjack 140792

Kind of:

a = [["beagle-status",255], ["bibtex",154312], ["bioradtopgm",7844],
["bison",242032], ["blackjack",140792]]
a.each do |item|
puts "#{sprintf('%-20s',item[0])} #{item[1]}"
end

Result:

beagle-status 255
bibtex 154312
bioradtopgm 7844
bison 242032
blackjack 140792

Regards.
 
M

Michael W. Ryder

Hi All,

How do I format the output from print or puts
so that columns are aligned:

So this:
beagle-status 255
bibtex 154312
bioradtopgm 7844
bison 242032
blackjack 140792


becomes this:

beagle-status 255
bibtex 154312
bioradtopgm 7844
bison 242032
blackjack 140792

You mean something like:

irb(main):001:0> puts "beagle-status".ljust(20) + "255".rjust(10)
beagle-status 255
=> nil
irb(main):002:0> puts "bibtex".ljust(20) + "154312".rjust(10)
bibtex 154312
=> nil
 
L

List.rb

How do I format the output from print or puts
so that columns are aligned:

So this:
beagle-status 255
bibtex 154312
bioradtopgm 7844
bison 242032
blackjack 140792

Kind of:

a =3D [["beagle-status",255], ["bibtex",154312], ["bioradtopgm",7844],
["bison",242032], ["blackjack",140792]]
a.each do |item|
puts "#{sprintf('%-20s',item[0])} #{item[1]}"
end

Result:

beagle-status 255
bibtex 154312
bioradtopgm 7844
bison 242032
blackjack 140792

Regards.


gem install ruport

require 'ruport'
puts Table:)data =3D> Hash[*a.compact])=
 

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

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top