开发者

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 repeate开发者_如何学编程d 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.


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


Use printf-style formatting:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜