0xab3d
0xab3d

Reputation: 557

Tabular output in ruby

I want to make tabular output in Ruby...

I am using puts "x\t\t[OK]

assuming that x represents inputted filename, and this process is repeated for ever, and assuming that the range of characters is from 5 - 20 characters the output won't be neat

Output sample: http://pastebin.com/kwJ9ajqj

I want the OKs to be aligned.

Upvotes: 0

Views: 917

Answers (2)

undur_gongor
undur_gongor

Reputation: 15954

You can fill the x-s with spaces to the same (maximum) length using ljust.

xs = [ "short", "longer string", "even a bit longer" ]
xs.each { | x | puts "#{x.ljust(20)} [OK]" }

This will align the "[OK]"s. If you need tabs, you can insert them after filling like

puts "#{x.ljust(20)}\t\t[OK]"

Upvotes: 2

bheeshmar
bheeshmar

Reputation: 3205

Use printf-style formatting:

printf("%20s  %s", filename, (ok ? '[OK]' : '[FAILED]'))

Upvotes: 0

Related Questions