简体   繁体   English

在Ruby中打印水平线

[英]Print Horizontal Line in Ruby

Can ruby's puts or print draw horizontal line kind of like bash does with printf+tr does ? ruby的puts或print可以像print一样用bash做水平线吗?

printf '%20s\n' | tr ' ' -

this will draw: 这将得出:

--------------------

You can use the following snippet 您可以使用以下代码段

puts "-"*20

Check this for more help. 检查以获得更多帮助。

You might be interested in formatting using ljust , rjust and center as well. 您可能也对使用ljustrjustcenter进行格式化感兴趣。

I use a quick puts "*"*80 for debug purposes. 我使用快速puts "*"*80进行调试。 I'm sure there are better ways. 我相信有更好的方法。

For fancy lines: 对于花式线:

p 'MY_LINE'.center(80,'_-')
#=> "_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-MY_LINE_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_"

You could also have the following: 您还可以具有以下内容:

puts "".center(20, "-")

irb(main):005:0> puts "".center(20, '-')
=> "--------------------"

This could be more flexible if you wanted to add additional information: 如果您想添加其他信息,这可能会更加灵活:

irb(main):007:0> puts "end of task".center(20, "-")
----end of task-----
=> nil

You can also use String#ljust or String#rjust . 您还可以使用String#ljustString#rjust

puts ''.rjust(20,"-")
# >> --------------------
puts ''.ljust(20,"-")
# >> --------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM