Can ruby's puts or print draw horizontal line kind of like bash does with printf+tr does ?
printf '%20s\n' | tr ' ' -
this will draw:
--------------------
Can ruby's puts or print draw horizontal line kind of like bash does with printf+tr does ?
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.
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
.
puts ''.rjust(20,"-")
# >> --------------------
puts ''.ljust(20,"-")
# >> --------------------