是否有与 C++ 等效的 Ruby? std::setw(int) 函数?

发布于 2024-12-03 04:23:56 字数 370 浏览 2 评论 0原文

我正在将一些文本表输出到终端,并且希望能够使用 C++ std::setw() 函数之类的功能为我的输出提供填充,而不是猜测所需的空格或制表符数量。在我开始拼凑一些东西来执行此操作之前,是否已经有一个函数或 Ruby Gem 可以执行此操作?

std::setw() 适合那些需要一些蜘蛛网流出的人(像我一样) http ://www.cplusplus.com/reference/iostream/manipulators/setw/

我目前使用的是 Ruby 1.8,因此最好有一个与其兼容的解决方案。

I am outputting some text tables to a terminal and would like to be able to use something like the C++ std::setw() function 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-12-10 04:23:56

这不是同一个接口(思维略有不同),但如果我正确理解目的,我通常只使用 sprintf 来实现此目的。

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

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       "
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文