[英]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: 这将得出:
--------------------
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#ljust
或String#rjust
。
puts ''.rjust(20,"-")
# >> --------------------
puts ''.ljust(20,"-")
# >> --------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.