Reputation: 557
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
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
Reputation: 3205
Use printf-style formatting:
printf("%20s %s", filename, (ok ? '[OK]' : '[FAILED]'))
Upvotes: 0