Printing columns

I’m trying to print some data to the terminal in columns and haven’t
seen a way to do it properly. I’ve tried “fred”.center(80) to try and
center the text at column 80 but when I start entering data before
“fred” it starts to push “fred” over to the right.

Is there a way to print at a specific column regardless of what text is
on the same line before hand or after?

I just worked it out, I was using a + instead of the proper , to
separate my text.

EG:
wrong way
print “hi” + “fred”.center(80)

proper way
print “hi”, “fred”.center(80)

Michael Pope wrote:

I’m trying to print some data to the terminal in columns and haven’t
seen a way to do it properly. I’ve tried “fred”.center(80) to try and
center the text at column 80 but when I start entering data before
“fred” it starts to push “fred” over to the right.

Is there a way to print at a specific column regardless of what text is
on the same line before hand or after?

Michael Pope wrote:

I’m trying to print some data to the terminal in columns and haven’t
seen a way to do it properly. I’ve tried “fred”.center(80) to try and
center the text at column 80 but when I start entering data before
“fred” it starts to push “fred” over to the right.

Is there a way to print at a specific column regardless of what text is
on the same line before hand or after?

Use an excape char sequence to move forward?

$ ruby -e ‘print “0123456789”*8; print “\r”; puts
“fred”.center(80).gsub(" ", “\e[C”)’
01234567890123456789012345678901234567fred23456789012345678901234567890123456789

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs