开发者

Is there a Ruby equivalent to the C++ std::setw(int) function?

开发者 https://www.devze.com 2023-04-02 07:39 出处:网络
I am outputting some text tables to a terminal and would like to be able to use something like the C++ std::setw() 开发者_如何学JAVAfunction to provide padding for my output rather than guessing at th

I am outputting some text tables to a terminal and would like to be able to use something like the C++ std::setw() 开发者_如何学JAVAfunction to provide padding for my output rather than guessing at the number of spaces or tabs required. Before I go knock together something to do this, is there already a function or Ruby Gem which does this?

Std::setw() for those who need a few cobwebs flow out (like me) http://www.cplusplus.com/reference/iostream/manipulators/setw/

I am using Ruby 1.8 at present so a solution compatible with that would be preferable.


It's not the same interface (so slightly different thinking), but if I understand the purpose correctly, I usually just use sprintf for this.

puts "%10s" % ["foo"]  # => "       foo"
puts "%-10s" % ["bar"] # => "foo       "
0

精彩评论

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

关注公众号