Centered output in console | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Centered output in console

I want that the ouput from something similar like "puts" or "print" is centered in the console. i currently havn't found a method or command for this in the internet. So: is there somethinglike this in Ruby and when yes, how I do that?

11th May 2018, 2:47 PM
Dragoncreator
Dragoncreator - avatar
1 Answer
+ 4
s = "dragoncreator" puts s.center(40) # can be modified to any value depending on the width of the console http://www.java2s.com/Code/Ruby/String/Centerleftjustifyandrightjustifystring.htm Edit: Just found out. It can also be done using printf, s = "dragon" printf "%24s\n", s
11th May 2018, 2:58 PM
Dev
Dev - avatar