Forum: Ruby formatting print output

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
870f5aa0e1f9e63f637d28b1c86f08b1?d=identicon&s=25 unknown (Guest)
on 2008-12-06 01:43
(Received via mailing list)
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
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2008-12-06 02:10
(Received via mailing list)
joemacbusiness@gmail.com wrote:
> 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
355dfec2a7633db603db2a178bddb631?d=identicon&s=25 Eustáquio Rangel (Guest)
on 2008-12-06 02:11
(Received via mailing list)
> 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.
990bf71a4e84e1145a3131f35656dc18?d=identicon&s=25 List Rb (listx300108u79872)
on 2008-12-06 02:31
(Received via mailing list)
On Dec 5, 2008, at 8:04 PM, "Eustáquio Rangel" <eustaquiorangel@gmail.co
m> wrote:

> Kind of:
> bibtex             154312
> bioradtopgm  7844
> bison              242032
> blackjack        140792
>
> Regards.
>


gem install ruport

require 'ruport'
puts Table(:data => Hash[*a.compact])
This topic is locked and can not be replied to.